Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View not attached to window manager, dialog dismiss

Tags:

android

So I've activity called GameActivity.java and in this activity I call DialogAnswer.show() which simple shows some picture on screen.

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:304)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at android.app.Dialog.dismissDialog(Dialog.java:325)
at android.app.Dialog.dismiss(Dialog.java:307)
at pl.evelanblog.prawdaczyfalsz.screen.DialogAnswer$1.onFinish(DialogAnswer.java:36)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

This is my DialogAnswer.java class

public class DialogAnswer extends Activity {

   private static ImageView resultImage;
   private static Dialog dialog = null;

   public static void show(Context context, boolean fCorrect) {

       dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
       dialog.setContentView(R.layout.dialog);
       resultImage = (ImageView) dialog.findViewById(R.id.result_image);

       if (fCorrect)
            resultImage.setImageResource(R.drawable.correct_image);
       else
            resultImage.setImageResource(R.drawable.incorrect_image);

       dialog.show();

        new CountDownTimer(700, 100) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
               dialog.dismiss(); //this is line 36
            }
        }.start();
        }
}

When the GameActivity.java sometimes when I going to another activity im getting error like this on top of my post. I dont know how to solve this, its hard to debug because its rare error but it exists.

like image 318
Jakub Pomykała Avatar asked Oct 23 '13 09:10

Jakub Pomykała


1 Answers

A lot of people may be googling this so I might put my 2p in:

Unfortunately the examples where people are using isShowing() aren't going to work as this can still return true when the view is detached (the activity has gone).

If you are lazy, the other posters comment about wrapping it in a try {} does also work in /most/ situations (though there are a few cases where the system may close it and an exception will still result in a force-close that you can't put a try{} round as it happens in android code, not yours)

The best solution is to close the dialogs when your activity finishes/closes. If you attempt to close it after the user navigates away whilst your async task is running (or, the phone rings and it's navigated away for them) then you're going to get the ViewNotAttached exception.

like image 173
piemmm Avatar answered Sep 28 '22 00:09

piemmm