4

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)
Xing Roy
  • 41
  • 1
  • 3
  • show the code where it occurs – himel Nov 23 '17 at 06:00
  • I had the same problem after updating my target API level to 26 in build.gradle. I rolled back to 25 and it works fine now. – Andrew Starostin Nov 29 '17 at 14:14
  • 1
    I have this problem too. The only way I can reproduce it is to put the `Toast` before `setContentView`. But I am still receiving this error report after I remove them. And I can't find a way to check if it's already running or not. – Kimi Chiu Nov 30 '17 at 04:19
  • 1
    Had the same problem, and my target API level is 26, too. – drakeet Dec 05 '17 at 07:42
  • 1
    As of now, Google does not allow uploading APKs with target API set to 25. I am still seeing this issue with customers in the field running Android 7.1. (My target API is 27). – Venu G. Feb 07 '19 at 08:02
  • I'm also facing the same BadTokenException. I'm using the Nexus 6 phone. I have a grid layout in a fragment and when I clicked one item( other fragments need to update) sometimes toast works but sometimes it gets crashed. I've changed my targetSDK to 26 too. – Sachz Aug 13 '20 at 17:21

5 Answers5

5

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

PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
  • From what you say, it looks the problem existed in Android 7.1 (API 25) but a fix was incorporated into Android 8.0 (API 26). Do you happen to know whether it was incorporated into any 7.1.X update? – Venu G. Feb 07 '19 at 08:12
  • 1
    I'm unaware if a fix was introduced in any updates for API 25, yet if Google did implement it then it would be the responsibility of each vendor to deploy it, which doesn't always happen. The safest is to use the ToastCompat library for such version. – PerracoLabs Feb 08 '19 at 10:33
3

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.

Olga Konoreva
  • 1,338
  • 13
  • 20
  • I get this error reported in production where leakcanary is inactive, so although it may be a possibility, it can also occur without it. – behelit Apr 27 '18 at 01:30
0

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.

g2server
  • 5,037
  • 3
  • 29
  • 40
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Sachz
  • 391
  • 5
  • 21
0

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()
}