Android 1.6:“android.view.WindowManager$BadTokenException:无法添加窗口——令牌 null 不适用于应用程序"; [英] Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"

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

问题描述

我正在尝试打开一个对话框窗口,但每次尝试打开它时都会抛出此异常:

I'm trying to open a dialog window, but every time I try to open it it throws this exception:

Uncaught handler: thread main exiting due to uncaught exception
android.view.WindowManager$BadTokenException: 
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(ViewRoot.java:460)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
  at android.app.Dialog.show(Dialog.java:238)
  at android.app.Activity.showDialog(Activity.java:2413)

我通过使用显示器的 id 调用 showDialog 来创建它.onCreateDialog 处理程序记录正常,我可以毫无问题地单步执行,但我已附加它,因为我似乎遗漏了一些东西:

I'm creating it by calling showDialog with the display's id. The onCreateDialog handler logs fine and I can step through it without an issue, but I've attached it since it seems like I'm missing something:

@Override
public Dialog onCreateDialog(int id)
{
    Dialog dialog;
    Context appContext = this.getApplicationContext();
    switch(id)
    {
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setContentView(R.layout.rename);
            dialog.setTitle("Rename " + noteName);
            break;
        default:
            dialog = null;
            break;
    }
    return dialog;      
}

这里有什么遗漏吗?一些问题谈到在从 onCreate 创建对话框时会出现这个问题,这是因为尚未创建活动,但这是来自菜单对象的调用,而 appContext 变量似乎在调试器中正确填充.

Is there something missing from this? Some questions have talked about having this problem when creating a dialog from onCreate, which happens because the activity isn't created yet, but this is coming from a call from a menu object, and the appContext variable seems like it is correctly populated in the debugger.

推荐答案

代替:Context appContext = this.getApplicationContext();您应该使用指向您所在活动的指针(可能是 this).

Instead of : Context appContext = this.getApplicationContext(); you should use a pointer to the activity you're in (probably this).

我今天也被这个咬了,烦人的部分是 getApplicationContext() 是来自 developer.android.com 的逐字记录:(

I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from developer.android.com :(

这篇关于Android 1.6:“android.view.WindowManager$BadTokenException:无法添加窗口——令牌 null 不适用于应用程序";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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