In recently, i always receive an error in android 7.1.1 that shows toast case crash. It's very strange, is anyone have the same problem?
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@b0baaa1 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:812)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:351)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.widget.Toast$TN.handleShow(Toast.java:489)
at android.widget.Toast$TN$2.handleMessage(Toast.java:360)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6475)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1134)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
The error happens when a Toast is shown with a context of an Activity that is no longer in the foreground. A solution is to show the Toast only by checking before if the activity is still active and not in a finishing state. This may not be always the best solution as there are scenarios where a Toast may be shown in a asynchronous way, such as within an asynchronous task, and appear once the Activity is no longer active, which will produce the crash.
The next library has an explanation of why it crashes, when the issue was introduced in Android, and solves the problem by catching the error:
https://github.com/drakeet/ToastCompat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With