【问题标题】:ProgressDialog not showing/only showing after asyncTaskProgressDialog 不显示/仅在 asyncTask 之后显示
【发布时间】:2018-05-17 22:39:43
【问题描述】:

我试图在单击调用 AsyncTask 的列表元素后向我的 UI 添加一个不确定的 progressDialog,但不幸的是,如果我像这样调用 onPreExecute 上的对话框:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    dialog.setProgressStyle(R.style.AppTheme_Dark_Dialog);
    dialog.setIndeterminate(true);
    dialog.setMessage(activity.getString(R.string.Buscando));
    dialog.show();
}

Obs:值得注意的是,Async 不是 Activity 类的子类,我将 Activity 作为参数传递给构造函数。

@Override
protected void onPostExecute(BuscaPendentesFechamento buscaPendentesFechamento) {
    super.onPostExecute(buscaPendentesFechamento);
    if(dialog.isShowing())
        dialog.dismiss();
}

对话框根本不显示,尽管它被创建和调用(已经检查过,活动实例是正确的)

如果我在活动本身上设置对话框,例如:

final ProgressDialog progressDialog = new ProgressDialog(RegistraPeso.this, R.style.AppTheme_Dark_Dialog);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage(getString(R.string.Buscando));
    progressDialog.show();
    BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(getApplicationContext());
    response = exportProjectRequisition.execute(nomeEquip,RegistraPeso.this);
    progressDialog.dismiss();

Dialog 显示在 Ui 上,但仅在 AsyncTask 已经执行之后,而不是在它被调用之前,并且根据代码,它应该在 Async 甚至被创建之前显示。

我能做什么?我究竟做错了什么?请帮忙。

编辑:在创建对话框的位置添加:

private ProgressDialog dialog;
private RegistraPeso activity;
BuscaPendentesFechamento(RegistraPeso activity) {
    this.dialog = new ProgressDialog(activity);
    this.activity = activity;
}

编辑:添加 doInBackground:

    @Override
protected BuscaPendentesFechamento doInBackground(String... params) {
    ArrayList<UmData> jsonArrayResponse = JSONParser.makeHttpRequestArray(
            Constants.URL_PENDENTES_FECHAMENTO,
            Constants.METHOD_POST, requisition.writeJSON(params[0]),
            requisition.getContext());
    if(jsonArrayResponse!=null)
    requisition.setJsonArrayResponse(jsonArrayResponse);
    else {
        UmData umData = new UmData();
        umData.setItemUm("Server not responding");
        jsonArrayResponse = new ArrayList<>();
        jsonArrayResponse.add(umData);
        requisition.setJsonArrayResponse(jsonArrayResponse);
    }
    return requisition;
}

【问题讨论】:

  • 将您的代码发布到您将上下文传递给 AsycTask 的位置。因为我没有在 onPreExecute() 中看到创建对话框。
  • 您是否检查过其他答案,例如 thisthis
  • 第二种解决方案没有用,因为它没有做值得做的事情。
  • 等等。 UI是如何冻结的?这是使用AsyncTask 的部分原因;所以用户界面不会冻结。您是否在实际代码中调用get()

标签: android android-asynctask progressdialog


【解决方案1】:

应用程序上下文在这种情况下不起作用。替换下面的行

 BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(getApplicationContext());

 BuscaPendentesFechamento exportProjectRequisition = new BuscaPendentesFechamento(YourActivity.this);

我看到你的代码在execute() 之后调用了dismiss()。删除此行。

 progressDialog.dismiss();

【讨论】:

  • 谢谢,但它什么也没改变 :(
  • 这不是问题,在第二个选项的情况下,我在 execute() 之后调用了dismiss(),对话框永远不会停止显示!
  • 你可能在代码的某个地方搞砸了。在 asynctask 中作为全局的活动引用。上面的代码应该可以工作。thx
【解决方案2】:

这里是OP,问题是通过根据[this][1]改代码解决的

[1]:How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? 在这里发帖,由@MikeM 发送给我,问题是我使用 .get() 从 AsyncTask 获得响应,并将 Async 转换为同步任务。 感谢麦克的支持!

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 2014-07-25
    • 1970-01-01
    • 2012-08-13
    • 1970-01-01
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多