問題描述
我知道這個問題已經討論過好幾次了,但相信我,我從來沒有得到任何有效的答案.在我的主要活動中,我使用了一些對話框來通知用戶一些操作,但我在小米設備上大多(特別是 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 無法添加窗口——令牌無效;您的活動正在運行嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!