How to find out what causes BadTokenExceptions

I am working on an Android application that has several activities and services. Some of the actions are defined in third-party libraries that I import into my project, and the problem is that on some devices (especially Samsung Galaxy Tabs), my application continues to crash when switching from one action to the previous using clicks on the back of the button.

I made LogCat and found this to be the cause of the crashes:

android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@351c808e is not valid; is your activity running? at android.view.ViewRootImpl.setView(ViewRootImpl.java:562) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:272) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

but what’s interesting is that none of my application classes appear in the stack trace. I searched for similar problems in StackOverflow, but every other report I found had a stack trace from a BadTokenException, the corresponding application code always displayed in the stack trace.

Does anyone know what are the common causes of this problem and / or the best way to fix it? I noticed that this problem is more apparent on Samsung devices, so maybe this is a real mistake on these devices.

+7
android
source share
1 answer

Usually BadTokenExceptions occurs when your activity tries to create a new Window before calling the onAttachToWindow() method (or after calling the onDetachFromWindow() method). It is possible that the third-party libraries that you use are errors and do not fulfill this requirement.

0
source share

All Articles