sunghun
sunghun

Reputation: 1424

ProgressDialog show error in AsyncTask

I am working on an AsyncTask to sign in the server but I am facing with error when I try to show progressdialog in it. However, it did work fine before. Here is my code.

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;
        }

    }
}

Log

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)

As you see, there is no special code in the AsyncTask. Can you help me to fix this problem? Any help would be appreciated.

Thanks.

Upvotes: 1

Views: 1828

Answers (3)

Sushil
Sushil

Reputation: 8478

May be you need to define mDialog in onPreExecute like this:

mDialog = new ProgressDialog(LoginTask.this);

Upvotes: 1

amalBit
amalBit

Reputation: 12181

The activity leaks a window when a dialog is called with its not in the foreground state. So to avoid this, we can stop the dialog in the onPause() of the activity.

Reason:
This is because a memory leak happens as the garbage collector is not able to free the memory allocated to the activity as the dialog is still attached to it. Thus the androidRuntime throws this error "Window leaked".

In your case:
Declare the mDialog as a global variable and kill it in the onPause:

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

Upvotes: 0

Dinesh Prajapati
Dinesh Prajapati

Reputation: 9510

The main reason for window leak is You're trying to show a Dialog after you've exited an Activity.

So Make sure when you are calling the onclick of the code you are not finishing the activity by anyway.

finish the activity only in post execute of the task if needed

Upvotes: 1

Related Questions