为什么我在asyncTask中使用AlertDialog获取NullPointerException?

dcp*_*450 0 android android-asynctask android-alertdialog

我有一个启动屏幕,运行asyncTask从API下载数据.在那个任务上OnPostExecute我运行下一个asyncTask发送存储的电子邮件.一旦完成,我需要AlertDialog弹出一个ok按钮,以便用户知道下载完成.我用这个问题得到了尽可能的:

AsyncTask里面的Android AlertDialog

现在,当我尝试向对话框添加属性时,我收到NullPointerException:

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();

        }
}
Run Code Online (Sandbox Code Playgroud)

错误即将来临 builder.setTitle("Sales Toolkit");

Bro*_*ins 5

AsyncTask#onPreExecute()不接受int参数.由于您的方法具有错误的签名,因此可能永远不会被调用,因此builder永远不会被设置.这是您应该使用@Override注释的典型示例.