使用对话框关闭函数时AsyncTask中的Invocationtargetexception

时间:2012-09-25 16:50:00

标签: android android-asynctask

我有关于将AsyncTask与ProgressDialog一起使用的问题。在调用dismiss方法之后有异常。我找到了许多相关问题,答案是“把它放在PostExecute()”中。但它不起作用......

public class Testing2 extends Activity {

private static final int PROGRESS = 0x1;

private ProgressBar mProgress;
private int mProgressStatus = 0;
private ProgressDialog dialog;
private Handler mHandler = new Handler();

protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    setContentView(R.layout.done);

    mProgress = (ProgressBar) findViewById(R.id.progressbar);

    new Testing().execute(1,2,3);


}
private class Testing extends  AsyncTask<Integer, Integer, Integer> {

       protected void onPreExecute()
       {
           ProgressDialog dialog = new ProgressDialog(this);
               dialog.setMessage("dasdasd");
               dialog.setIndeterminate(true);
               dialog.setCancelable(false);
               dialog.show();

       }
     protected Integer doInBackground(Integer... urls) {

           Integer totalSize = 1;

           return totalSize;
       }

       protected void onProgressUpdate(Integer... progress) {

       }

       protected void onPostExecute(Integer result) {
           dialog.dismiss();
       }
   }
}

1 个答案:

答案 0 :(得分:0)

您正在将ProgressDialog分配给本地变量,而不是分配给字段dialog
而不是

ProgressDialog dialog = new ProgressDialog(this);

使用

dialog = new ProgressDialog(Testing2.this);

如果你在doinbackground中做得不多,我怀疑进度条会出现。如果你想测试功能,至少在doinbackground中进行2秒的睡眠调用