我正在尝试打开一个对话框窗口,但每次我尝试打开它时都会引发此异常:
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
我可以毫无问题地逐步完成它,但我已经附上了它,因为我似乎遗漏了一些东西:
@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
变量似乎已正确填充在调试器中。