6

I am getting below crash exclusively at app launch time and only in Emulator & Debug

Emulator & Run : OK

Device & Run: OK

Device & Debug: OK

Emulator & Debug: CRASH!

It looks like Android is trying to do a Toast at launch time, and as I don't have many Toast and having added a breakpoint on all of them, I can exclude that it is one of mine.

Is there a way to find out what message Android is trying to show and to which Activity it is referring? Logcat doesn't show anything abnormal before the crash occurs.

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e4f4f2b is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at android.widget.Toast$TN.handleShow(Toast.java:459)
        at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
michaelsmith
  • 951
  • 1
  • 14
  • 32
  • not much activity here...? – michaelsmith Oct 24 '18 at 11:07
  • Can you provide the code, where it is occuring – abitcode Oct 27 '18 at 03:58
  • Try switching off Instant run and run the code. – Saikrishna Rajaraman Oct 27 '18 at 04:02
  • Well, there is no code of mine where it happens. Stacktrace is 100% Android. It happens when app is launched. OnCreate and OnResume have been called and have completed all tasks without problem. Instant Run is off – michaelsmith Oct 27 '18 at 06:13
  • show the relevant code of the activity/ies for when this is happening. Any dialogs, toasts, anything that launches an activity or secondary window –  Oct 27 '18 at 12:43
  • There is simply no code for dialogs or toasts launched from an activity anywhere. The crash doesnt happen on the device or when not run in Debug. I am pretty sure it doesnt come from my code but Android tries to send something to my activity. How can I know which message it is trying to send? – michaelsmith Oct 27 '18 at 13:41
  • Can you run on emulator and then connect debugger? Will it work? And please add code for your launch activity. And this info might be useful for you: [WindowManager$BadTokenException](https://stackoverflow.com/a/18665887/3940713) – Mykhailo Oct 27 '18 at 18:04
  • That was a useful idea! Attaching the debugger after Run on Emulator works and app stops at all my breakpoints. No idea why launching in Debug on Emulator crashes, but I can live with doing it that way. Possibly a bug in AS? – michaelsmith Oct 28 '18 at 15:16
  • Facing same problem like you @michaelsmith – ZarNi Myo Sett Win Jan 25 '19 at 08:44

1 Answers1

3

This is most likely happening because you are trying to show a dialog after the execution of a background thread, while the Activity is being destroyed.

Here's what worked for me:

if(!((Activity) context).isFinishing())
{
    //show dialog
}

It's working perfectly for me, using this practice for years now.

aanshu
  • 1,502
  • 10
  • 12