token android.os.BinderProxy@e4f4f2b is not valid; is your activity running?
Asked Answered
F

1

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)
Fluorite answered 23/10, 2018 at 12:56 Comment(9)
not much activity here...?Fluorite
Can you provide the code, where it is occuringDismember
Try switching off Instant run and run the code.Audet
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 offFluorite
show the relevant code of the activity/ies for when this is happening. Any dialogs, toasts, anything that launches an activity or secondary windowStrew
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?Fluorite
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$BadTokenExceptionBarouche
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?Fluorite
Facing same problem like you @FluoriteBlankly
T
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.

Turk answered 29/10, 2018 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.