【问题标题】:my progress dialog remains open in android我的进度对话框在 android 中保持打开状态
【发布时间】:2014-04-22 12:58:36
【问题描述】:

我已经制作了一个异步任务,用于从 Web 服务获取一些 Json 响应。我在后台处理完成时使用了一个进度条,并在我的 asynctask 的 onPostExecute 方法中完成了进度对话框。事情是我得到了成功的响应根据需要,但之后 myl 进度对话框仍然可见,请任何人告诉我如何关闭它。我的代码如下: ma​​in.java

私有类 DoFavourite 扩展 AsyncTask {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    // Showing progress dialog
    pDialog = new ProgressDialog(ProductDetailActivity.this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);
    pDialog.show();

}

@Override
protected Void doInBackground(Void... arg0) {
    String favUrl = Const.API_DO_FAVOURITE + "?product_id=" + pid + "&customer_id=" + Pref.getValue(ProductDetailActivity.this, Const.PREF_CUSTOMER_ID, "");
    System.out.println(":::::::::my FAVOURITE URL::::::::::::::" + favUrl);
    // Creating service handler class instance
    BackendAPIService sh = new BackendAPIService();

    // Making a request to url and getting response
    String jsonStr = sh.makeServiceCall(favUrl, BackendAPIService.GET);

    Log.d("Response: ", "> " + jsonStr);
    System.out.println("=============MY RESPONSE==========" + jsonStr);

    if (jsonStr != null) {
        try {
            JSONObject jsonObj = new JSONObject(jsonStr);
            if (jsonObj.has(Const.TAG_STATUS)) {
                if (jsonObj.getString(Const.TAG_STATUS).equalsIgnoreCase("success")) {
                    if (jsonObj.getString(Const.TAG_FAVOURITE).equalsIgnoreCase("1")) {
                        flag = 1;
                    } else {
                        flag = 2;
                    }

                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }

    return null;
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // Dismiss the progress dialog
    if (pDialog.isShowing())
        pDialog.dismiss();

}

}

【问题讨论】:

  • 请登录 onPostExecute(Void result) 方法并检查您的情况
  • 条件有什么问题?一切都好吗?
  • 替换 if (pDialog.isShowing()) pDialog.dismiss();只需 pDialog.dismiss()

标签: android android-asynctask progressdialog


【解决方案1】:

您没有正确使用进度对话框。您会注意到 IDE 在您的 pd.show(...) 行旁边显示了一个简洁的小警告标志。

你在做什么

使用 new ProgressDialog() 创建一个(不可见、不相关的)进度对话框

使用 pd.Show() 创建另一个带有所需文本的进度对话框,而不存储对它的引用。

关闭第一个对话框。 (2) 中的对话框仍然存在。

如果您将代码替换为:

//pd = new ProgressDialog(this); 

pd = ProgressDialog.show(this, "Waiting...", "Please wait five seconds..."); 

【讨论】:

  • 你能编辑我的吗?代码,我没有正确得到你的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2013-11-14
  • 1970-01-01
相关资源
最近更新 更多