android.view.WindowManager$BadTokenException: 无法添加窗口 -- 在 Toast [英] android.view.WindowManager$BadTokenException: Unable to add window -- at Toast

查看:37
本文介绍了android.view.WindowManager$BadTokenException: 无法添加窗口 -- 在 Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的 android 应用程序上频繁执行某些操作时(我的假设是由于 Toast 消息),出现以下错误.我没有得到这个问题的确切位置.我可以从别人那里得到帮助来解决这个问题吗?

When I do some action frequently(my assumption, is that due to Toast message) on my android app,getting below error.Im not getting exact location of this Issue.Can I get help from someone to resolve the same?

 --------- beginning of crash
10-04 16:13:49.250 6541-6541/com.test.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.myapp, PID: 6541
    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@e2815e is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
        at android.widget.Toast$TN.handleShow(Toast.java:459)
        at android.widget.Toast$TN$2.handleMessage(Toast.java:342)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method) 

注意:这种崩溃并不总是发生.

Notes: This crash is not coming always.

推荐答案

在将 Context 传递给 Toast 之前,您应该始终检查将要使用的上下文的有效性.在我的应用程序中,我使用了我制作的上下文检查器方法:

Before passing a Context to the Toast, you should always check the validity of the context you are going to use. In my applications, I use a context-checker method I made:

public static boolean isContextValid(Context context, Fragment fragment) {
    if (context instanceof Activity) {
        Activity activity = (Activity) context;
        if (activity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed())) {
            return false;
        }
    }

    return context != null && (fragment == null || (fragment.isAdded() && !fragment.isRemoving());
}

您可以只传递一个上下文,或者如果您当前的上下文是一个片段,也可以传递一个 Fragment.此方法检查上下文是否为 Activity,在这种情况下,我们检查 Activity 是否正在完成/销毁.

You can pass only a context, or also a Fragment if your current context is a fragment. This method checks if the context is an Activity, in this case we check if the activity is finishing/destroyed.

如果你想在fragment生命周期之后显示toast,也传递给你当前fragment的方法,这样我们就可以判断fragment是否仍然可见并附加到activity中.

If you want to display the toast following fragment lifecycle, also pass to the method you current fragment, so we can tell whether the fragment is still visible and attached to the activity.

Android 7.1 奖励

在 API 25 上,这还不够,有时设备仍会因您提供的堆栈跟踪而崩溃.

On API 25, this is not enough and sometimes the device still crash with the stacktrace you provided.

这个存储库 可能是解决方案,因为它将错误调用包装在 try/catch 子句中.当然这不是最好的做法,但至少解决了 7.1 设备的这个恼人的崩溃问题.

This repository might be the solution since it wraps the faulty call in a try/catch clause. Of course it's not the best thing to do, but at least solves this annoying crash for 7.1 devices.

这篇关于android.view.WindowManager$BadTokenException: 无法添加窗口 -- 在 Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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