Xing Roy
Xing Roy

Reputation: 41

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?

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)

Upvotes: 4

Views: 3943

Answers (5)

Cherno Omar Senghore
Cherno Omar Senghore

Reputation: 27

I experienced a similar problem in an android app I was working on in which I call a new activity from the the single choice selection I made from the Alert Dialogbox. This error occured because execution of code happened asynchronously so the new activity was started and the activity where the Alert Dialog Box was defined is finished. That means the context has changed. If you look at error message properly, it points at the line "alertDialog.show()"

So what you should do is to check if your activity (where alert dialog box is defined) is finishing before showing alert. Look at the code below:

if (!isFinishing) {
  alertDialog.show()
}

Upvotes: 0

Sachz
Sachz

Reputation: 411

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:

  1. 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 ) enter image description here

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

    if(!mService.getItem(mac).isLive()){
    
        //Functions() to pass the selected items relevant information to the 2 other fragments
    …………
    ……….
    
        Toast.makeText(this, "Selected: " + name + "\n" + Add, Toast.LENGTH_SHORT).show();
    
        Log.d();
    } else{
        Toast.makeText(this, "Selected: " + name + " " + "is LIVE. Unable to select", Toast.LENGTH_SHORT).show();
    }
    

    }

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

Upvotes: 0

Perraco
Perraco

Reputation: 17340

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

Upvotes: 5

g2server
g2server

Reputation: 5367

Try wrapping the Toast with this:

if(!getActivity().isFinishing())
{
    //show toast
}

EDIT:

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

Upvotes: 0

Olga Konoreva
Olga Konoreva

Reputation: 1438

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.

Upvotes: 3

Related Questions