Android view windowmanager badtokenexception unable to add windows

android.view.WindowManager$BadTokenException: Unable to add window — token android.os.BinderProxy@b0baaa1 is not valid; is your activity running?

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?

4 Answers 4

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:

Do you use Leak Canary? There’s an open issue associated with Toast messages.

It looks like Leak Canary is trying to notify a user about the leak using no longer existing context.

Try wrapping the Toast with this:

EDIT:

If you are using the activity context, try changing the context of the Toast to getApplicationContext() instead.

I also faced the error android.view.WindowManager$BadTokenException. My activity is running on a Service and I have a grid view in the first fragment. When I clicked one item relevant information is passed to the other 2 fragments also a Toast is appeared when an item is selected. Here I received ( sometimes works without exception — this is strange though) the BadTokenException and app got crashed. I’m using the Nexus 6 phone.

My Answer:

I changed targetSdkVersion from 29 to 26. ( PS: I’m not exactly sure whether this step is correct. Since my intention is to use the app only for Nexus 6 I changed this from 29 to 26 )

Читайте также:  What is the purpose linux

Then I moved Toast to the bottom of all the functions. (Initially, my toast was at the top of all the functions.) Now Toasts appear after fragment updates.

@Override public void onItemDeviceSelected(String mac, String name) <

At the moment I don’t receive the exception (thankfully). I tried selecting the grid item several times.

“android.view.WindowManager$BadTokenException: Unable to add window” on buider.show()

From my main activity , I need to call an inner class and in a method within the class, I need to show AlertDialog . After dismissing it, when the OK button is pressed, forward to Google Play for purchase.

Things work perfectly for most of the times, but for few users it is crashing on builder.show() and I can see «android.view.WindowManager$BadTokenException: Unable to add window» from crash log. Please suggest.

My code is pretty much like this:

I have also seen the error in another alert where I am not forwarding to any other activity . It’s simple like this:

10 Answers 10

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.

Reason :

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.

Solution:

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).

Читайте также:  Ati radeon hd 4670 drivers windows 10

eg.

Update :

Window Tokens:

As its name implies, a window token is a special type of Binder token that the window manager uses to uniquely identify a window in the system. Window tokens are important for security because they make it impossible for malicious applications to draw on top of the windows of other applications. The window manager protects against this by requiring applications to pass their application’s window token as part of each request to add or remove a window. If the tokens don’t match, the window manager rejects the request and throws a BadTokenException. Without window tokens, this necessary identification step wouldn’t be possible and the window manager wouldn’t be able to protect itself from malicious applications.

A real-world scenario:

When an application starts up for the first time, the ActivityManagerService creates a special kind of window token called an application window token, which uniquely identifies the application’s top-level container window. The activity manager gives this token to both the application and the window manager, and the application sends the token to the window manager each time it wants to add a new window to the screen. This ensures secure interaction between the application and the window manager (by making it impossible to add windows on top of other applications), and also makes it easy for the activity manager to make direct requests to the window manager.

android.view.WindowManager$BadTokenException: Unable to add window

I’m adding popup window in fragment when a user clicks on floating action button, in popup window I have added AutoComplete Edit field when I clicked the floating action button, the popup window shows successfully but when I click the edit field it gave an error. The error is

Here is complete error.

Here is my code

1 Answer 1

Reason of Exception: 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.

Solution of Exception: Please use isFinishing() method which is called by Android to check whether this activity is in the process of finishing:

Not the answer you’re looking for? Browse other questions tagged android popupwindow or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Софт для windows планшета

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@c745883 — permission denied

Here’s my stack trace:

I found an answer about TYPE_SYSTEM_ERROR being deprecated in Android Oreo (8) so I implemented the following method that I also found:

Inside of my onCreate() method I have:

I still get the error.

I am using Expo SDK 21, React Native 0.48. The application has been detached to ExpoKit.

After each change I clean my project and then run it on an Emulator through Android Studio.

Edit: I am running this on a Nexus 5X emulator, running on API 27.

3 Answers 3

I saw this error some time ago, when I updated a native&react app to SDK 26. The problem is the react-native function that creates the red dialog for development. That functions uses TYPE_SYSTEM_ALERT as their type, so you can’t use an SDK level bigger than 25 in your hybrid app with that version of react-native, unless you patch that function so it no longer uses TYPE_SYSTEM_ALERT.

this is the react-native code in 0.48:

You would need to change the mRedBoxDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); call to other type. But, this does not give you any guarantees you’ll be able to run your app with SDK 26, when I tried to compile react with SDK 26, other parts of the project «reacted» exploding, so this is unlikely to work at short term (you may need to start fixing other problems). So your options are:

-Downgrade the app to level 25;

-Upgrade to react 0.52, where that function no longer exist, and try again (react libraries may not work after this)

-Patch the function in react-native 0.48 branch and try the patched version. Some other issues related to the SDK may arose (react at this point is still being compiled with SDK 22)

Оцените статью