Android“无法添加窗口——令牌空值不适用于应用程序"异常 [英] Android 'Unable to add window -- token null is not for an application' exception

查看:32
本文介绍了Android“无法添加窗口——令牌空值不适用于应用程序"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试打开对话框时出现以下 Android 异常.有人可以帮我了解发生了什么,我该如何解决这个问题?

I get the following Android exception when I try to open a dialog. Can someone please help me understand what is going on and how can I fix this problem?

android.view.WindowManager$BadTokenException: 
  Unable to add window -- token null is not for an application
    at android.view.ViewRoot.setView(ViewRoot.java:509)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    at android.app.Dialog.show(Dialog.java:241)

推荐答案

我在猜测 - 您是否正在尝试使用应用程序上下文创建 Dialog?像这样:

I'm guessing - are you trying to create Dialog with an application context? Something like this:

new Dialog(getApplicationContext());

这是错误的.您需要使用活动上下文.

This is wrong. You need to use an Activity context.

你必须尝试:

new Dialog(YourActivity.this);

这篇关于Android“无法添加窗口——令牌空值不适用于应用程序"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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