ProgressDialog在AsyncTask中显示错误

时间:2013-11-15 04:38:48

标签: android android-asynctask progressdialog

我正在使用AsyncTask登录服务器,但是当我尝试在其中显示progressdialog时,我遇到了错误。但是,之前它确实运行良好。这是我的代码。

LoginActivity.java

public class LoginActivity extends Activity {
public void loginButtonClick(View view) {
  //TODO.Validatioin check here

  LoginTask loginTask = new LoginTask();
  loginTask.execute(app.getPortalWsUrl(app.AUTHENTICATION_URI) , getUserId(), getPassword());
}

private class LoginTask extends AsyncTask<String, String, JsonResult<Subscriber>> {

        private SimpleHttpClient mClient;
        private ProgressDialog mDialog;

        public LoginTask(){
            this.mClient = new SimpleHttpClient();
            this.mDialog = new ProgressDialog(LoginActivity.this);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mDialog.setTitle(R.string.m_logging);
            mDialog.setMessage(LoginActivity.this.getString(R.string.m_wait));
            mDialog.show();
        }

        @Override
        protected JsonResult<Subscriber> doInBackground(String... params) {
            JsonResult<Subscriber> jsonResult = new JsonResult<Subscriber>();

            if (params.length!=3) {
                //jsonResult.setMessage("Invalid arguments to call method.");
                return null;
            }

            String url  = params[0];
            String userId   = params[1];
            String password = params[2];

            try {
                StringBuilder sb = new StringBuilder();
                sb.append(url)
                  .append("?")
                  .append("userid=").append(userId)
                  .append("&")
                  .append("password=").append(password);

                String receivedText = mClient.postJson(sb.toString(), "");

                JSONObject jsonObj = new JSONObject(receivedText);
                jsonResult.setOk(jsonObj.optBoolean("ok"))
                          .setMessage(jsonObj.optString("message"));

                if (jsonResult.isOk()){
                    JSONObject result = jsonObj.optJSONObject("result");
                    SubscriptionService service = parseResult(result);
                    Subscriber subsriber = new Subscriber();
                    subsriber.userId = userId;
                    subsriber.password = "";
                    subsriber.userName = result.optString("accountHolder");
                    subsriber.service = service;
                    jsonResult.setResult(subsriber);
                }

            } catch (Exception e) {
                logger.error(e.getMessage()); //==>This was problem.
                jsonResult.setOk(false).setMessage(e.getMessage());
            }

            logger.debug("###JsonResult###" + jsonResult);

            return jsonResult;
        }

        @Override
        protected void onPostExecute(JsonResult<Subscriber> result) {
            super.onPostExecute(result);
            if (mDialog.isShowing())
                mDialog.dismiss();
            //TODO.LoginDone(result)
        }

        /**
         * @param value
         * @return
         * @throws UnsupportedEncodingException 
         */
        private String encode(String value) throws UnsupportedEncodingException{
            return URLEncoder.encode(value, "UTF-8");
        }

        private SubscriptionService parseResult(JSONObject jsonObj) throws JSONException{
                         SubscriptionService service = new SubscriptionService();
            //Do soething
            return service;
        }

    }
}

日志

11-15 15:21:37.910: E/WindowManager(1748): Activity stalker.activity.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ffa4e0 that was originally added here
11-15 15:21:37.910: E/WindowManager(1748): android.view.WindowLeaked: Activity stalker.activity.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40ffa4e0 that was originally added here
11-15 15:21:37.910: E/WindowManager(1748):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
11-15 15:21:37.910: E/WindowManager(1748):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
11-15 15:21:37.910: E/WindowManager(1748):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
11-15 15:21:37.910: E/WindowManager(1748):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
11-15 15:21:37.910: E/WindowManager(1748):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
11-15 15:21:37.910: E/WindowManager(1748):  at android.app.Dialog.show(Dialog.java:278)
11-15 15:21:37.910: E/WindowManager(1748):  at stalker.activity.LoginActivity$LoginTask.onPreExecute(LoginActivity.java:262)
11-15 15:21:37.910: E/WindowManager(1748):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
11-15 15:21:37.910: E/WindowManager(1748):  at android.os.AsyncTask.execute(AsyncTask.java:511)
11-15 15:21:37.910: E/WindowManager(1748):  at activity.LoginActivity.onLoginButtonClick(LoginActivity.java:126)

如您所见,AsyncTask中没有特殊代码。你能帮我解决这个问题吗? 任何帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:1)

窗口泄漏的主要原因是您在退出活动后尝试显示对话框。

因此,请确保在调用代码的onclick时,无论如何都没有完成活动。

仅在需要后执行任务时完成活动

答案 1 :(得分:1)

可能需要在onPreExecute中定义mDialog,如下所示:

mDialog = new ProgressDialog(LoginTask.this);

答案 2 :(得分:0)

当调用对话框时,活动会泄漏窗口,而不是处于前台状态。所以为了避免这种情况,我们可以在活动的onPause()中停止对话。

<强>原因:
  这是因为垃圾回收器无法释放分配给活动的内存,因为对话框仍然附加到它,因此发生内存泄漏。因此androidRuntime抛出此错误“Window泄露”。

在您的情况下:
 将 mDialog 声明为全局变量并在onPause中将其终止:

    @Override
    protected void onPause() {
      if(progressDialog != null)
        progressDialog.dismiss();
      super.onPause();
    }