我知道这个问题已经讨论过好几次了,但相信我,我从来没有得到任何有效的答案.在我的主要活动中,我使用了一些对话框来通知用户一些操作,但我在小米设备上大多(特别是 96%)得到这个特定的错误.`
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)<代码>我介绍了每个 Dialog Show 方法,例如
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:794)
I covered each Dialog Show method like
if (!MainActivityNew.this.isFinishing())
dialogDownload.show();
`但我仍然在发布模式下遇到此错误,主要是在 Xiaomi 设备上使用 Android 版本 7,请帮我解决过去一个月的这个问题.
` 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.
这是来自 firebase Crashlytics 的一些数据
Here is some data from firebase Crashlytics
设备100% 小米操作系统100% 安卓 7设备状态4% 背景
Devices 100% Xiaomi Operating systems 100% Android 7 Device states 4% background
这个问题不是因为Dialog,而是Toast.主要是这个 Toast 问题在上下文变为空的 android 7.1 版本中.这位先生创建了一个解决方案 Solution .我已经测试过了,效果很好.你可以试试这个.
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();
}
这篇关于致命异常:android.view.WindowManager$BadTokenException 无法添加窗口——令牌无效;您的活动正在运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!