致命异常:android.view.WindowManager $ BadTokenException无法添加窗口 - 令牌无效;你的活动在运行吗? [英] Fatal Exception: android.view.WindowManager$BadTokenException Unable to add window -- token is not valid; is your activity running?

查看:249
本文介绍了致命异常:android.view.WindowManager $ BadTokenException无法添加窗口 - 令牌无效;你的活动在运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经讨论好几次,但相信我,我从来没有得到任何答案。
在我的主要活动中,我使用了少量的Dialog来告知用户一些操作,但我在小米设备上主要(特别是96%)得到了这个特定的错误。
`

 致命异常:android.view.WindowManager $ BadTokenException 
无法添加窗口 - 令牌android.os.BinderProxy@f839de9无效;你的活动在运行吗?
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方法,如

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

`
但我仍然在发布模式下出现此错误主要是在小米设备
使用Android版本7,请帮助我在过去一个月内坚持这个问题。



以下是来自firebase Crashlytics的一些数据<设备状态
4%背景/ p>

解决方案

这个问题不是因为Dialog而是Toast。主要是这个Toast问题是在android 7.1版本中,其中上下文变为null。这位先生解决方案创建了一个解决方案。我已经测试过,这种方法非常有效。你可以试试这个。

  if(android.os.Build.VERSION.SDK_INT == 25){
ToastCompat.makeText(context,消息,Toast.LENGTH_SHORT)
.setBadTokenListener(toast - > {
Log.e(失败的toast,消息);
})。show();
} else {
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
}


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

解决方案

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无法添加窗口 - 令牌无效;你的活动在运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆