0

I am using an AsyncTask to create progress dialog and then start a new activity. The code is :-

package com.android.grad;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;

public class LoginTask extends AsyncTask<Void, Void, Boolean> {

    private Activity activity;
    private ProgressDialog pd;
    private Uri fileUri;
    private final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;

    public LoginTask(Activity activity, Uri fileUri) {
        this.activity = activity;
        this.fileUri = fileUri;
    }

    @Override
    protected void onPreExecute() {
        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        pd = ProgressDialog.show(activity, "Signing in",
                "Please wait while we are signing you in..");
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        pd.dismiss();
        pd = null;
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        activity.startActivityForResult(intent,
                CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }   
}

and this task is start when i press a button using this Action listener.

private OnClickListener loginOnClick = new OnClickListener() {
        public void onClick(View v) {
            new LoginTask(LoginActivity.this, fileUri).execute();
        }
    };

and i got that error when the orientation change.

06-25 20:41:27.199: E/WindowManager(8867): Activity com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here
06-25 20:41:27.199: E/WindowManager(8867): android.view.WindowLeaked: Activity com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:344)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.Window$LocalWindowManager.addView(Window.java:537)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.Dialog.show(Dialog.java:278)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:116)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:99)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ProgressDialog.show(ProgressDialog.java:94)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.grad.LoginTask.onPreExecute(LoginTask.java:25)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.AsyncTask.execute(AsyncTask.java:511)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.grad.LoginActivity$1.onClick(LoginActivity.java:36)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.View.performClick(View.java:3527)
06-25 20:41:27.199: E/WindowManager(8867):  at android.view.View$PerformClick.run(View.java:14234)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Handler.handleCallback(Handler.java:605)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 20:41:27.199: E/WindowManager(8867):  at android.os.Looper.loop(Looper.java:137)
06-25 20:41:27.199: E/WindowManager(8867):  at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 20:41:27.199: E/WindowManager(8867):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 20:41:27.199: E/WindowManager(8867):  at java.lang.reflect.Method.invoke(Method.java:511)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 20:41:27.199: E/WindowManager(8867):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 20:41:27.199: E/WindowManager(8867):  at dalvik.system.NativeStart.main(Native Method)
06-25 20:41:27.309: D/dalvikvm(8867): GC_CONCURRENT freed 265K, 4% free 9339K/9671K, paused 6ms+4ms
06-25 20:41:27.519: D/AndroidRuntime(8867): Shutting down VM
06-25 20:41:27.519: W/dalvikvm(8867): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
06-25 20:41:27.529: E/AndroidRuntime(8867): FATAL EXCEPTION: main
06-25 20:41:27.529: E/AndroidRuntime(8867): java.lang.IllegalArgumentException: View not attached to window manager
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:587)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:324)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:151)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.Dialog.dismissDialog(Dialog.java:321)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.Dialog$1.run(Dialog.java:119)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Handler.handleCallback(Handler.java:605)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.os.Looper.loop(Looper.java:137)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at java.lang.reflect.Method.invokeNative(Native Method)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at java.lang.reflect.Method.invoke(Method.java:511)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 20:41:27.529: E/AndroidRuntime(8867):     at dalvik.system.NativeStart.main(Native Method)

I searched on the site and i didn't find the solutions that worked with me

@Override
    protected void onPreExecute() {
        //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        pd = ProgressDialog.show(activity.getApplicationContext(), "Signing in",
                "Please wait while we are signing you in..");
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        pd.dismiss();
        pd = null;
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        activity.startActivityForResult(intent,
                CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }   

06-25 21:23:59.709: E/AndroidRuntime(10041): FATAL EXCEPTION: main
06-25 21:23:59.709: E/AndroidRuntime(10041): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:517)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.Dialog.show(Dialog.java:278)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:116)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:99)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ProgressDialog.show(ProgressDialog.java:94)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.grad.LoginTask.onPreExecute(LoginTask.java:25)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.AsyncTask.execute(AsyncTask.java:511)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.grad.LoginActivity$1.onClick(LoginActivity.java:38)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.View.performClick(View.java:3527)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.view.View$PerformClick.run(View.java:14234)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Handler.handleCallback(Handler.java:605)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.os.Looper.loop(Looper.java:137)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at java.lang.reflect.Method.invokeNative(Native Method)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at java.lang.reflect.Method.invoke(Method.java:511)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-25 21:23:59.709: E/AndroidRuntime(10041):    at dalvik.system.NativeStart.main(Native Method)
Mahmoud Emam
  • 1,499
  • 4
  • 20
  • 37

3 Answers3

1

On rotation, the Activity is destroyed, and so is your ProgressDialog, therefore, when you try to hide the dialog, it doesn't exist anymore and generates this exception.

theelfismike
  • 1,621
  • 12
  • 18
  • Okay, How can i handle that exception ? How to save the state of the dialog and when the orientation change? – Mahmoud Emam Jun 25 '12 at 18:49
  • In your activity, you'll want to override onSaveInstanceState/onRestoreInstanceState. Likely, you'll want to keep a reference to your AsyncTask and pull out the relevant state to save/restore via the Bundle – theelfismike Jun 25 '12 at 18:51
1
 com.android.grad.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@2be69ff8 that was originally added here

1- as looks you are using Activity reference to show dialog only so instead of passing the Activity cto constructor pass Application context to it to show dialog ....

2- pd.dismiss(); pd = null;should be in onPostExecute on in doInBackground with check like isShowing or any one better..........

 If(null!=pd && pd.isShowing ()){
      pd.dismiss();

       pd = null;
    }
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • where to write these two lines in onPost or doInback ? – Mahmoud Emam Jun 25 '12 at 18:59
  • When i used Application Context the dialog doesn't appear and give Exception 06-25 21:23:59.709: E/AndroidRuntime(10041): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application – Mahmoud Emam Jun 25 '12 at 19:25
  • http://stackoverflow.com/questions/1561803/android-progressdialog-show-crashes-with-getapplicationcontext – Dheeresh Singh Jun 25 '12 at 19:42
  • try to pass the activity.getApplicationContext() to instead of fetching after public LoginTask(Context context, Uri fileUri) – Dheeresh Singh Jun 25 '12 at 19:46
1

Check the Is AsyncTask really conceptually flawed or am I just missing something?

It provides a deep investigation of this issue and a nice solution on how to keep a running asynctask across activity restars caused by device configuration changes (orientation changes, etc).

Community
  • 1
  • 1
Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91