- android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@a59d688 — permission denied for window type 2038 #6
- Comments
- piotrek-chmielowski commented Jan 19, 2018
- sockeqwe commented Jan 19, 2018
- javierdiaziut commented Jan 24, 2019
- 0xpulsar commented May 7, 2019
- 0xpulsar commented May 7, 2019
- 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
android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@a59d688 — permission denied for window type 2038 #6
Comments
piotrek-chmielowski commented Jan 19, 2018
In my Fragment I have the following code:
In the AndroidManifest.xml:
And I’m getting following error:
Android version: Oreo.
Am I doing something wrong or is it a bug?
The text was updated successfully, but these errors were encountered:
sockeqwe commented Jan 19, 2018
Since Android 7 you explicitly have to grant a permission «Draw over other apps» for apps not installed over play store (which is the case for debug builds, right?)
javierdiaziut commented Jan 24, 2019
I am facing the same problem, how did you solved it?
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@7a40cfc — permission denied for window type 2038
at android.view.ViewRootImpl.setView(ViewRootImpl.java:817)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:330)
at com.app.health.daytoday_android.framework.NotificationService.launchDialog(NotificationService.kt:527)
at com.app.health.daytoday_android.framework.NotificationService.onStartCommand(NotificationService.kt:101)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3565)
at android.app.ActivityThread.-wrap21(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1742)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
0xpulsar commented May 7, 2019
@javierdiaziut, even I am getting the same problem. Did you get the solution now?
0xpulsar commented May 7, 2019
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.
Источник
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.
Источник