我试图打开一个对话框窗口,但每次我试图打开它时,它都会抛出这个异常:
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
处理程序日志很好,我可以在没有问题的情况下逐级执行它,但我附加了它,因为它似乎缺少一些东西:
@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
变量似乎在调试器中被正确填充。