简体   繁体   English

为什么在asyncTask中通过AlertDialog收到NullPointerException?

[英]Why am I getting a NullPointerException with AlertDialog in an asyncTask?

I have a splash screen that runs an asyncTask that downloads data from an API. 我有一个运行asyncTask的启动屏幕,该asyncTask从API下载数据。 On that task's OnPostExecute I run the next asyncTask to send stored emails. 在该任务的OnPostExecute我运行下一个asyncTask来发送存储的电子邮件。 Once that is complete I need an AlertDialog to popup with an ok button so the user knows the downloads are complete. 完成此操作后,我需要使用OK按钮弹出AlertDialog ,以便用户知道下载已完成。 I used this SO question to get as far as I have: 我用这个SO问题来达到我的目的:

Android AlertDialog inside AsyncTask AsyncTask中的Android AlertDialog

Now I'm getting a NullPointerException when I attempt to add properties to the dialog: 现在,当我尝试向对话框中添加属性时,我得到了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();

        }
}

The error is coming up on builder.setTitle("Sales Toolkit"); 错误发生在builder.setTitle("Sales Toolkit");

AsyncTask#onPreExecute() doesn't take an int argument. AsyncTask#onPreExecute()不采用int参数。 Since your method has the wrong signature, it is likely never being called, and therefore builder is never set. 由于您的方法签名错误,因此很可能永远不会调用它,因此永远不会设置builder This is a classic example of why you should use @Override annotations. 这是为什么应使用@Override注释的经典示例。

Do not use a .get() to execute an AsyncTask as it will not be async anymore. 不要使用.get()执行AsyncTask,因为它将不再是异步的。 And onPreExecute will not be called? 并且onPreExecute将不会被调用?

It seems like the onPreExecute() method is not being called. 似乎未调用onPreExecute()方法。 If you don't really need to use the builder anywhere before the onPostExecute() method, I would suggest just moving 如果您真的不需要在onPostExecute()方法之前的任何地方使用builder ,建议您移动

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

into the onPostExecute() method. 进入onPostExecute()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM