Android的“无法添加窗口 - 令牌null不是一个应用程序的例外 [英] Android 'Unable to add window -- token null is not for an application' exception

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

问题描述

我,当我尝试打开一个对话框,得到以下的Andr​​oid例外。是否有人可以帮助我了解是怎么回事,我该如何解决这个问题?

  android.view.WindowManager $ BadTokenException:
  无法添加窗口 - 令牌null不是一个应用程序
    在android.view.ViewRoot.setView(ViewRoot.java:509)
    在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
    在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
    在android.app.Dialog.show(Dialog.java:241)
 

解决方案

我猜 - 你想用的应用程序上下文中创建对话框?事情是这样的:

 新的对话框(getApplicationContext());
 

这是错误的。您需要使用一个活动环境。

你必须尝试这样的:

 新的对话框(YourActivity.this);  

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)

解决方案

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.

You have to try like:

new Dialog(YourActivity.this);

这篇关于Android的“无法添加窗口 - 令牌null不是一个应用程序的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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