16

I am getting the following remotely from clients so I don't know what hardware etc they are using.

java.lang.IllegalArgumentException: View not attached to window manager
       at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
       at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
       at android.view.Window$LocalWindowManager.removeView(Window.java:417)
       at android.app.Dialog.dismissDialog(Dialog.java:279)
       at android.app.Dialog.access$000(Dialog.java:72)
       at android.app.Dialog$1.run(Dialog.java:108)
       at android.app.Dialog.dismiss(Dialog.java:263)
       at com..mysite.android.ActivityGame$1.onFinish(ActivityGame.java:154)
       at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:123)
       at android.app.ActivityThread.main(ActivityThread.java:4203)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:521)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)

This is happening because of a ProgressDialog

    progressDialog = new ProgressDialog( this );
    progressDialog.setMessage(getString(R.string.get_ready));
    progressDialog.setCancelable(false);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progressDialog.setMax(12);
    progressDialog.show();

    new CountDownTimer(3000, 250) {

         @Override
        public void onTick(long millisUntilFinished) {
             //progressDialog.incrementProgressBy(1);
         }

         @Override
        public void onFinish() {
             progressDialog.dismiss(); //********* ERROR HAPPENS HERE *********
             nextQuestion();
         }
    }.start();

The Activity looks like this in the Manifest.

<activity android:theme="@style/GameTheme" android:name=".ActivityGame" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation"></activity>

So what could this mean? I think it has something to do with the Activity being destroyed then created but as you can see I have the configChanges set correctly.

jax
  • 37,735
  • 57
  • 182
  • 278

6 Answers6

22

Try:

if (pDialog.isShowing()) {
   pDialog.cancel();
}

in your overridden onDestroy() or onStop() methods.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Ajay
  • 4,850
  • 2
  • 32
  • 44
  • 4
    Surprisingly in some the exception can still appear. I have a null check and isShowing check and I still get the exception when I change the orientation. – ılǝ May 14 '13 at 09:07
13

This problem arises when trying to show a dialog after you've exited an Activity.

I just solved this problem just by writing down the following code:

public void onDestroy(){
super.onDestroy();
if(progressDialog!=null)
if(progressDialog.isShowing()){
progressDialog.cancel();
}

}

Basically, from which class you started progressDialog, override onDestroy() method and do this way. It solved Activity has leaked window problem.

biegleux
  • 13,179
  • 11
  • 45
  • 52
Shoaib Ahmed
  • 757
  • 7
  • 7
4

It usually happens when you call dismiss after activity has been closed.

Fedor
  • 43,261
  • 10
  • 79
  • 89
2

In order to handle invisible views, you cannot use isShowing() and should check for the window attachment itself in your onDestroy()

if (pDialog.getWindowToken() != null)
{
   pDialog.dismiss();
}

or in my case, I was not using a dialog but a custom window added by the WindowManager, which was (possibly) invisible.

if (test_service_overlay != null) {
     if (test_service_overlay.getWindowToken() != null) {
         WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
         windowManager.removeViewImmediate(test_service_overlay);
     }
}
Joel Teply
  • 3,260
  • 1
  • 31
  • 21
1

Im also faced the same problem when I tried to dismiss the dialog at onPageFinished method of Web view. sometimes onPageFinished called after the activity has been closed.

Here is the solution for it:

    if(pDialog.isShowing())
    {
      try
      {
        pDialog.dismiss();
      }
      catch(Exception e) {// nothing }

    }

Try it!

Shahul3D
  • 2,129
  • 1
  • 15
  • 16
0

You can ask "dialog.isIndeterminate()" as well. Works fine.

oskarko
  • 3,382
  • 1
  • 26
  • 26