0

我有关于将 AsyncTask 与 ProgressDialog 一起使用的问题。在我调用解除方法后出现异常。我发现了很多与此相关的问题,答案是“把它放在 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();
       }
   }
}
4

1 回答 1

0

您正在将 ProgressDialog 分配给局部变量而不是字段dialog
所以而不是

ProgressDialog dialog = new ProgressDialog(this);

采用

dialog = new ProgressDialog(Testing2.this);

如果您在 doinbackground 中没有做太多事情,我怀疑进度条会出现。如果您想测试功能,请在 doinbackground 中至少拨打 2 秒钟的睡眠电话

于 2012-09-25T16:53:55.157 回答