安卓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"
问题描述
我想打开一个对话窗口,但每次我尝试打开它,它抛出这个异常:
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)
我通过调用的ShowDialog
与显示器的ID创建它。该 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.
推荐答案
即。这条线: 背景appContext = this.getApplicationContext(); 一定要去,而是你用一个指针,指向你在(可能这一点)的活动。
i.e. this line: Context appContext = this.getApplicationContext(); must go, and instead you 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 :(
这篇关于安卓1.6:" android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不是一个申请表]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!