AsyncTask onPostExecute not being called

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

The project I'm working on is slightly more complicated but I made this simple test to try to track down what was wrong with my code. The progress dialog never dismisses. I had it at one point where they weren't returning null. '

public class SyncTestActivity extends Activity {     /** Called when the activity is first created. */     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         new mTask(this).execute();     }      public class mTask extends AsyncTask<Void, Void, Void> {          Context mContext;          ProgressDialog progressDialog;          public mTask(Context aContext) {             mContext = aContext;         }          @Override         public void onPreExecute() {              progressDialog = new ProgressDialog(mContext);             progressDialog.setMessage("New...");             progressDialog.show();         }          @Override         public Void doInBackground(Void... params) {             return null;         }            public Void onPostExecute(Void... params) {             progressDialog.dismiss();             return null;           }     }  } 

回答1:

The parameters are wrong, use this:

    @Override     protected void onPostExecute(Void result) {         progressDialog.dismiss();         return;      } 


回答2:

I am agree with Cesar and Shailendra answers, but still let me make little improvement over it:

    @Override     protected void onPostExecute(Void result) {        if(progressDialog.isShowing())       {         progressDialog.dismiss();       }         return;      } 


回答3:

Missing @Override notation before onPostExecute. Also return null is not required.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!