【问题标题】:Android: Progress dialog not shown at 1st time (using Thread or AsyncTask for longDuration operation)Android:第一次未显示进度对话框(使用 Thread 或 AsyncTask 进行 longDuration 操作)
【发布时间】:2012-10-27 16:25:22
【问题描述】:

我的应用程序在 AsyncTask 中的进度对话框出现问题: 我的AsyncTask 正在doInBackGround() 中从互联网上获取数据,我正在onPreExecute() 中开始进度对话框,在onPostExecute() 中完成对话框。

我在单击按钮时执行此 asyncTask。第一次单击此按钮(发生长时间操作)时,未显示进度对话框。但下次进度对话框会正常显示。

第一次运行AsyncTask,没有显示进度对话框可能是什么问题?

这是 onClick 方法:

public void onClickFind(View view){
 new Task_Search().execute();
}

这是任务Task_Search

private class Task_Search extends AsyncTask<Void, Void, Void> {

 @SuppressWarnings("deprecation")
 protected void onPreExecute() {
  showDialog(DIALOG_TASKING);
 }

 protected Void doInBackground(Void... unused) {
  //some geting of web content here
 }

 @SuppressWarnings("deprecation")
 protected void onPostExecute(Void unused) {
  dismissDialog(DIALOG_TASKING);
 }
}

MainActivity中定义的进度对话框:

@SuppressWarnings("deprecation")
@Override
protected Dialog onCreateDialog(int id) {
 switch (id) {
 case DIALOG_TASKING:
  mSearchDialog = new ProgressDialog(this);
  mSearchDialog.setMessage("Searching for field number..");
  mSearchDialog.setCancelable(true);
  return mSearchDialog;
  }
 return super.onCreateDialog(id);
}

在使用AsyncTask之前,我使用单独的线程来下载数据:在onClick中,我先启动progressDialog然后线程,当线程完成时progressDialog也停止了, 行为是相同的: 第一次点击按钮对话框没有显示,下次显示。

谁能帮忙?

【问题讨论】:

    标签: android multithreading android-asynctask progressdialog


    【解决方案1】:

    我看不到你在对话框中调用 show()。

    替换

    @SuppressWarnings("deprecation")
    protected void onPreExecute() {
        showDialog(DIALOG_TASKING);
    }
    

    @SuppressWarnings("deprecation")
    protected void onPreExecute() {
        showDialog(DIALOG_TASKING).show();
    }
    

    【讨论】:

    • 是的,我把它误认为是用户定义的方法。我建议在任何情况下都不要使用 showDialog,而是创建我自己的 ProgressDialog 并显示它。
    • 我同意,我也推荐!
    • 我换了,还是一样。
    【解决方案2】:

    不要调用 showDialog insode onPreExecute()。 AsyncTask 提供了 publishProgress() 方法,可以从 doInBackground() 调用该方法以在 UI 线程上发布更新。

    【讨论】:

    • onPreExecute() 也在 UI 线程上运行,并且出于这个原因提供。
    • 我从未说过它不在 UI 线程上运行。很明显,他想在对话框中显示 doInBackground() 完成的进度。 onPreExecute 在 doInBackground 之前运行,所以他怎么能在那里显示对话框。在了解问题和答案之前,请不要投反对票
    • 在 onPreExecute 中显示 ProgressDialog 不需要来自 doInBackground 的任何信息。更新进度本身确实可以,但这与它的显示无关。大多数 AsyncTask/ProgressDialog 示例都是这样做的,甚至 Google 也推荐它。阅读 The 4 Steps 下的here
    • 哦,我的错。如果您只想显示来自 onPreExecute 的对话框,@Geobits 是正确的。我想知道它是否与“@override”符号有关。
    • AsyncTask 正是用于这些目的。所以你的答案是错误的
    【解决方案3】:

    好的, 我在 MainActivity 中创建了我自己的方法:

    public void showProgressDialog() {
     if (mProgressDialog == null) {
      mProgressDialog = new ProgressDialog(this);
      mProgressDialog.setMessage("Searching for field number..");
      mProgressDialog.show();
      return;
     }else{
      mProgressDialog.setMessage("Searching for field number..");
      mProgressDialog.show();
      return;
     }
    

    }

    还有:

    public void closeProgressDialog() {
     if (mProgressDialog != null) {
      mProgressDialog.dismiss();
     }
     return;
    }
    

    我更改了 AsyncTask 如下:

    private class Task_Search extends AsyncTask<Void, Void, Void> {
     protected void onPreExecute() {
      // showDialog(DIALOG_TASKING);
      showProgressDialog();
     }
    
     protected Void doInBackground(Void... unused) {
     //Some long duration stuff here
     }
    
     protected void onPostExecute(Void unused) {
      //dismissDialog(DIALOG_TASKING);
      closeProgressDialog();
     }
    }
    

    mProgressDialog 是 MainActivity 的私有成员

    private ProgressDialog mProgressDialog;
    

    情况相同:点击按钮:第一次 ProgressDialog 卡住,下一次完美运行

    【讨论】:

    • 卡住是什么意思?它不是显示不确定的旋转吗?
    • 第一次使用操作不显示(点击按钮操作+旋转进度对话框出现),下次显示..
    • 我还没有发现可能是什么问题,我也在真实的硬件、HTC Sensation 和三星 Galaxy Pad 2 上进行了测试,与 AVD 中的类似
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2018-04-15
    相关资源
    最近更新 更多