9

Can anyone explain to me why this error occures, or even better how do I handle it? I can not reproduce it. It is one of those errors that happends once out of a 1000.

Background: The user is trying to log in, a progress dialog is showing, a http request is sent in async task, the progress dialog is dissmissed. Error occures, app FC.

LoginActivity.java

 255:   private void dismissProgress() {  
 256:     if (mProgress != null) {  
 257:         mProgress.dismiss();  
 258:         mProgress = null;  
 259:     }  
 260:   }  

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:391)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:236)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at se.magpern.LoginActivity.dismissProgress(LoginActivity.java:257)
at se.magpern.LoginActivity.access$5(LoginActivity.java:255)
at se.magpern.LoginActivity$DoTheLogin.onPostExecute(LoginActivity.java:293)
at se.magpern.LoginActivity$DoTheLogin.onPostExecute(LoginActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
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:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
EboMike
  • 76,846
  • 14
  • 164
  • 167
Magnus
  • 332
  • 1
  • 4
  • 10

2 Answers2

15

This can happen if the user either dismisses the view (e.g. a dialog that can be backed out of) or if the user switches to a different activity while your task is running. You should seriously think about using Android's activity-native dialog showing/dismissing instead of trying to keep a reference to the views yourself. But if you are handling it yourself, you may want to check if the dialog is actually showing using the dialog's isShowing() method before trying to dismiss it.

Yoni Samlan
  • 37,905
  • 5
  • 60
  • 62
6

I've seen this happen when a latent update comes in for a progress dialog that has already been fully or partially dismissed. Either the user is requesting a dismissal at the same time the os is trying to dismiss the view and its already been disconnected from the window or vice versa.

There is a race condition between the code that dismisses the progress window when a button is clicked and the code that dismisses the progress window in another fashion. The mostly likely place to look for this race condition is where your requests for dismissing the windows are put onto the view thread (button handler or a code callback).

Nick Campion
  • 10,479
  • 3
  • 44
  • 58
  • Thanks. I'll try some more testing to see if I can interrupt the dialog in someway. Currently the user can't dismiss the dialog, and the dialog is not using any progress updates (it's just spinning). The dismissal is done in the onPostExecute so it should be in same thread – Magnus Feb 15 '11 at 00:00
  • 2
    @Magnus Could the race condition be caused by an orientation change occuring at the same time that the `AsyncTask` attempts to dismiss the dialog? – dave.c Feb 15 '11 at 00:29
  • As everyone is pointing out, there are lots of ways this can go wrong, including orientation changes. You can note that because we see AsyncTask in your stack that it is coming to the party second, so something else (orientation change, dismissal by user) has already gotten there. – Nick Campion Feb 15 '11 at 02:35
  • Yes! It struck me before I read your comments. It happends om orientation changes. I have suppressed orientation changes in other views, but not this one. Now that I know what it is, a solution is near. Thanks! – Magnus Feb 15 '11 at 15:22