0

I have a splash screen that runs an asyncTask that downloads data from an API. On that task's OnPostExecute I run the next asyncTask to send stored emails. Once that is complete I need an AlertDialog to popup with an ok button so the user knows the downloads are complete. I used this SO question to get as far as I have:

Android AlertDialog inside AsyncTask

Now I'm getting a NullPointerException when I attempt to add properties to the dialog:

public class JSONParser extends AsyncTask<String, String, JSONObject> {
     Context c;

     public JSONParser(int api,Context c) {
          this.api= api;
          this.c = c;
     }
     ...
     protected void onPostExecute(JSONObject result) {
          JSONObject output = new JSONEmailParser(c).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new String[] {null,null,null,null}).get();
     }
}

public class JSONEmailParser extends AsyncTask<String, String, JSONObject> {
     Context c;

     AlertDialog.Builder builder;

     public JSONEmailParser(Context c){
          this.c = c;
     }

     protected void onPreExecute(int api){
          builder = new AlertDialog.Builder(SplashScreen.this);
     }

     ...

     protected void onPostExecute(JSONObject result) {
          setLastUpdate();

          builder.setTitle("Sales Toolkit");
          builder.setCancelable(false);
          builder.setMessage("Download Complete");
          builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {

               @Override
               public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                    endSplash();
               }
          });
          builder.show();

        }
}

The error is coming up on builder.setTitle("Sales Toolkit");

Community
  • 1
  • 1
dcp3450
  • 10,959
  • 23
  • 58
  • 110
  • You should always use the @Override annotation. It helps catch errors like this. – JDJ Jul 02 '14 at 20:16

3 Answers3

5

AsyncTask#onPreExecute() doesn't take an int argument. Since your method has the wrong signature, it is likely never being called, and therefore builder is never set. This is a classic example of why you should use @Override annotations.

Brodo Fraggins
  • 628
  • 5
  • 14
0

Do not use a .get() to execute an AsyncTask as it will not be async anymore. And onPreExecute will not be called?

greenapps
  • 11,154
  • 2
  • 16
  • 19
-1

It seems like the onPreExecute() method is not being called. If you don't really need to use the builder anywhere before the onPostExecute() method, I would suggest just moving

builder = new AlertDialog.Builder(SplashScreen.this);

into the onPostExecute() method.

Clarkarot
  • 116
  • 1
  • 9