- Android BadTokenException: Unable to add window, is your activity running
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid; is your activity running? #92
- Comments
- epool commented Aug 14, 2020
- skydoves commented Aug 15, 2020 •
- epool commented Aug 17, 2020 •
- skydoves commented Oct 7, 2020
- znakeeye commented Oct 16, 2020
- sembozdemir commented Oct 19, 2020
- skydoves commented Nov 7, 2020
- znakeeye commented Nov 10, 2020
- WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@bf4921f is not valid;
- Problem description
- Reproduce the bug
- Problem solving
- Exception android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid; is your activity running? #26
- Comments
- ripern commented Apr 12, 2017
Android BadTokenException: Unable to add window, is your activity running
I faced this exception in one of my application and after a long research I found a solution for this problem.
This exception occurs when the app is trying to notify the user from the background thread (AsyncTask) by opening a Dialog.
If you are trying to modify the UI from background thread (usually from onPostExecute() of AsyncTask) and if the activity enters finishing stage i.e.) explicitly calling finish(), user pressing home or back button or activity clean up made by Android then you get this error.
android.view.WindowManager$BadTokenException: Unable to add window — token [email protected] is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:547)
at android.app.Dialog.show(Dialog.java:277)
com.ibc.activityasyncdemo.MainActivity$LongTask.onPostExecute(MainActivity.java:72)
com.ibc.activityasyncdemo.MainActivity$LongTask.onPostExecute(MainActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Let us first create this scenario which causes the exception to be thrown, by using the following code. Here I want to show an AlertDialog in onPostExecute method of my AsyncTask. I pass my activity’s reference to the task’s constructor using “this”, storing it in an instance and using it to create a dialog.
If the activity finishes (either by calling finish() or if user presses back/home button) before the task completes, alertDialog.show() throws android.view.WindowManager$BadTokenException: Unable to add window — token [email protected] is not valid; is your activity running?.
The reason for this exception is that, as the exception message says, the activity has finished but you are trying to display a dialog with a context of the finished activity. Since there is no window for the dialog to display the android runtime throws this exception.
To solve this problem, we can use isFinishing() method which is called by Android to check whether this activity is in the process of finishing: be it explicit finish() call or activity clean up made by Android. By using this method it is very easy to avoid opening dialog from background thread when activity is finishing.
Also maintain a weak reference for the activity (and not a strong reference so that activity can be destroyed once not needed) and check if the activity is not finishing before performing any UI using this activity reference (i.e. showing a dialog).
Weak references are useful for mappings that should have their entries removed automatically once they are not referenced any more (from outside).
To check this, uncomment the finish() statement in onCreate() and run this program. The dialog will not pop up because there is no reference to the activity since it has been finished.
Источник
android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid; is your activity running? #92
Comments
epool commented Aug 14, 2020
Please complete the following information:
- Library Version [v1.1.9]
- Affected Device(s) [General]
Describe the Bug:
We are receiving this crash in our crashlytics console
This is a dummy implementation of our view for a reference implementation:
Expected Behavior:
Don’t receive these crashes.
The text was updated successfully, but these errors were encountered:
skydoves commented Aug 15, 2020 •
Hi, @epool !
Could you test using this way?
I think your Activity is destroyed but still seems to show and reference the destroyed Activity’s context.
Or the custom view is used in a fragment?
epool commented Aug 17, 2020 •
@skydoves yes, it’s being used in an Activity. The reason why I’m not using myLifeCycle.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) is because the custom view is being added and removed from its parent programmatically and when it’s added again after being removed I want to still listening the events, otherwise after the Lifecycle.Event.ON_DESTROY the custom view stops receiving the view model events. I’ll try to use the activity as lifecycleOwner = . The weird thing is that the custom view is declared inside of the activity and has the same life cycle scope in theory.
skydoves commented Oct 7, 2020
Hi, @epool!
Could you let me know about this issue going on?
znakeeye commented Oct 16, 2020
This could happen if you show a tooltip in some event handler where the event is handled just after e.g. user has pressed the back button.
I tried safe-guarding by checking fragment.isAdded() and possibly also (don’t remember) fragment.isRemoving() but it still crashed. My fix was to skip the tooltip entirely.
For sure, we need some extra checks when showing the balloon. Will try to isolate this in a sample project at some point. Did the OP solve this?
sembozdemir commented Oct 19, 2020
This issue is also happening for my app. Stacktrace is the same. I use it in an activity. I also use LiveData.
skydoves commented Nov 7, 2020
@znakeeye @sembozdemir
I released a new version 1.2.5.
This update includes preventing to show popups in activities when the window is already detached from the activities.
Please let me know if the same crash continues. Thanks!
znakeeye commented Nov 10, 2020
@skydoves @sembozdemir
So far, I have not been able to reproduce this error in my setup. Very encouraging! Would be great if someone could confirm the fix in a published app.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Источник
WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@bf4921f is not valid;
Problem description
A product guy said it was easy to collapse when entering into an interface, checked the log and found that indeed there is an exception, sporadic bug actually, the following is the error log
Reproduce the bug
From the log, can probably understand what is the reason, here wrote a demo to reproduce this bug. XML is relatively simple, not listed, the current Activity inherited a BaseActivity, in fact, do nothing. Note that the following code is just for demo, do not copy and paste.
Problem solving
The reason for the crash is simple, when it came to show dialog, the activity attached to the dialog had been hung up, so the program crashed.
It’s easy to solve the problem when you know the reason.
We just need to modify the isLiving method in the DialogUtil above, as follows
The above code is easy to understand, notice the code I’m commenting on, and there’s a method, isDestroyed(), which is added from version 4.2. why would I commenton it?
Look at the code in front of it. There’s a isFinishing() method in it. What’s the difference?
According to my print, in onPause(), onStop() and onDestroy() methods, the return value of isFinishing() method is true; In onCreate(), onStart() and onResume() methods, the return value is false.
And the return value of isDestroy(), only in onDestroy() method, it’s ture, the other life cycle methods, false.
In that case, as long as the isFinishing() is written in front, write or not to write the isDestroyed() method is the same.
Posted by suang in Android at May 14, 2017 — 9:16 PM
Источник
Exception android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid; is your activity running? #26
Comments
ripern commented Apr 12, 2017
I’ve gotten a bunch of crash reports for this error since I released a new version of an app last week which included Tooltip for the first time.
Exception android.view.WindowManager$BadTokenException: Unable to add window — token null is not valid; is your activity running? android.view.ViewRootImpl.setView (ViewRootImpl.java:850) android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:337) android.view.WindowManagerImpl.addView (WindowManagerImpl.java:91) android.widget.PopupWindow.invokePopup (PopupWindow.java:1329) android.widget.PopupWindow.showAsDropDown (PopupWindow.java:1155) android.widget.PopupWindow.showAsDropDown (PopupWindow.java:1114) android.widget.PopupWindow.showAsDropDown (PopupWindow.java:1093) com.tooltip.Tooltip$2.run (Tooltip.java:211) android.os.Handler.handleCallback (Handler.java:739) android.os.Handler.dispatchMessage (Handler.java:95) android.os.Looper.loop (Looper.java:158) android.app.ActivityThread.main (ActivityThread.java:7224) java.lang.reflect.Method.invoke (Method.java) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
After some quick googling I found another tooltip library that had a very similar error in June 2016. It was fixed by checking the root view, douglasjunior/android-simple-tooltip@b8bc55f
As I’m getting the crashes on line 211 in your Tooltip lib I would assume a similar check there would solve this, or what do you say? https://github.com/ViHtarb/Tooltip/blame/master/library/src/main/java/com/tooltip/Tooltip.java#L211
Thanks for a great lib otherwise!
The text was updated successfully, but these errors were encountered:
Источник