13

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.

Jakub Pomykała
  • 2,082
  • 3
  • 27
  • 58
  • This question is a dupe, and the answers naive, so to help others who stumble upon this question, the best summary of solutions I've found so far is here: https://androidresearch.wordpress.com/2013/05/10/dealing-with-asynctask-and-screen-orientation/ – Nick Westgate May 22 '15 at 06:10
  • You can take Refference from [Here](https://stackoverflow.com/questions/2745061/java-lang-illegalargumentexception-view-not-attached-to-window-manager/45346389#45346389) – Shashwat Gupta Jul 27 '17 at 09:20

5 Answers5

28

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.

piemmm
  • 383
  • 2
  • 5
  • 3
    "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" an example ? – xmen Mar 02 '14 at 15:47
  • 2
    best answer ever for this question! – ar-g Dec 09 '14 at 11:29
18

Before dismissing check like this in onDestroy() or onStop() method..You are simple dismissing not checking whether it is showing or not

if (mDialog!=null) {
    if (mDialog.isShowing()) {
        mDialog.dismiss();       
    }
}
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • 3
    Is there any difference if I place the two statements in one condition? `if (null != pDialog && pDialog.isShowing()) { pDialog.dismiss(); }` -> This triggered `IllegalArgumentException: View not attached to window manager` – Compaq LE2202x Jun 04 '14 at 02:55
  • Inside `onPostExecute` of my Asynctask, I wondered why it stilled triggered the exception. – Compaq LE2202x Jun 04 '14 at 05:20
  • Don't forget on `super.onDestroy` or `super.onPause` – Ali Bdeir Aug 04 '16 at 19:16
4

Use a try statement.

new CountDownTimer(700, 100) {
        public void onTick(long millisUntilFinished) {
        }
        public void onFinish() {
           try {
               dialog.dismiss();
               dialog = null;
          } catch (Exception e) {
               //TODO: Fill in exception
          }
        }
    }.start();
frogmanx
  • 2,620
  • 1
  • 18
  • 20
  • 12
    Don't know how can this helps exactly, you're just patching the problem (which is not optimized at all since handling exceptions is slow) instead of giving a real solution. – Aballano Dec 11 '13 at 10:08
  • I'm guessing it helped by directly fixing the OP's problem/bug, do you have a 'real' solution to this problem? – frogmanx Dec 11 '13 at 11:20
  • I ran into the same problem as the poster previously, and that answer didn't work. I believe the poster, and myself, encountered a different scenario in which that condition was met, but still caused an exception to be thrown. – frogmanx Dec 11 '13 at 20:23
  • See comments [java.lang.IllegalArgumentException: View not attached to window manager](http://stackoverflow.com/a/5102572/465942). Bad idea to just swallow exceptions like this. – Kapé Sep 15 '14 at 19:16
2

DO this way

new CountDownTimer(700, 100) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        dialog.dismiss(); //this is line 36

                    }
                });
            }
        }.start();
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
2

using try catch may not be a efficient way to solve this problem as may cause memory leak; for this question, as the context is used as param, so before using the code dialog.dismiss, we can use codes below to protect:

public void onFinish() {
   if{ctx instanceof Activity && !((Activity)ctx.isfinishing()){
           dialog.dismiss(); //this is line 36
   }
}

also, another method can be used to patch this crash in the function of onDestroy() in the activity, add the code:

protected void onDestroy() {
  if(dialog != null){
     dialog.dismiss();
     dialog = null;
  }
}
JNYong
  • 71
  • 4