12

I know this question is discussed several time but believe me i never got any answer working. In my Main Activity i have used few Dialog to inform user about some operations but i get this specific error mostly (specifically 96%) on Xiaomi devices. `

Fatal Exception: android.view.WindowManager$BadTokenException
Unable to add window -- token android.os.BinderProxy@f839de9 is not valid; is your activity running?
android.view.ViewRootImpl.setView (ViewRootImpl.java:697)
android.view.WindowManagerGlobal.addView (WindowManagerGlobal.java:347)
android.view.WindowManagerImpl.addView (WindowManagerImpl.java:94)
android.widget.Toast$TN.handleShow (Toast.java:463)
android.widget.Toast$TN$2.handleMessage (Toast.java:346)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:163)
android.app.ActivityThread.main (ActivityThread.java:6377)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:904)

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:794) I covered each Dialog Show method like

if (!MainActivityNew.this.isFinishing())
dialogDownload.show();

` But still i am getting this error in release mode mostly on Xiaomi Devices With Android Version 7, Please help me i stuck on this problem from past one month.

Here is some data from firebase Crashlytics

Devices 100% Xiaomi Operating systems 100% Android 7 Device states 4% background

6
  • see this link :- stackoverflow.com/questions/9529504/… Commented Jul 26, 2018 at 6:38
  • 1
    thanks Rajshree, but i have already mentioned i have used isFinishing() method as stated in above answer. Commented Jul 26, 2018 at 6:42
  • From where you are firing the showToast method? is it from Actiivty or AsyncTask or any other background thread? Commented Jul 26, 2018 at 7:05
  • It is from Main UI Thread Commented Jul 26, 2018 at 7:07
  • @SantoshPatange have you managed to handle it? I have a similar problem but for Motorola E4 and Android 7.1.1 Commented Sep 4, 2018 at 8:26

2 Answers 2

24

This problem is not because of Dialog but with Toast. Mainly this Toast issue is in android 7.1 version where the context become null. There is a solution created by this gentleman Solution . I have tested and this is working very well. You can try this.

 if (android.os.Build.VERSION.SDK_INT == 25) {
                ToastCompat.makeText(context, message, Toast.LENGTH_SHORT)
                        .setBadTokenListener(toast -> {
                            Log.e("failed toast", message);
                        }).show();
            } else {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
Sign up to request clarification or add additional context in comments.

3 Comments

I am having the same issue in android 7.1 .. in my case it is popupwindow. i am trying to display spinner as dropdown inside popupwindow. which causes crash on android 7.1 only. spinner as dialog is working fine. but i need dropdown. can anyone help me out please.
Hello @AlpeshGujarati - identical problem here: a Spinner inside a PopupWindow crashes, actually not only on Android 7.1, but on 7.1 and everything below (I've tested API versions 23,24,25 --> all crash, everything >=26 works fine) No solution yet...
Actually android:spinnerMode="dialog" helps!
-3

Use Asynctask or Handler like:

Use private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
  case DISPLAY_DLG:
    if (!isFinishing()) {
    dialogDownload.show();
    }
    break;
  }
 }
};

1 Comment

Like author stated - he checks whether activity isFinishing right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.