尝试打开对话框时出错:android.view.WindowManager $ BadTokenException

时间:2010-08-23 23:58:58

标签: android dialog

我尝试从Android活动中打开对话框时收到此错误。 Google搜索没有提供任何有用的解释。希望有人知道这个例外甚至意味着什么?

08-21 00:42:48.515: ERROR/AndroidRuntime(880): Uncaught handler: thread main exiting due to uncaught exception
08-21 00:42:48.595: ERROR/AndroidRuntime(880): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.ViewRoot.setView(ViewRoot.java:429)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:178)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.app.Dialog.show(Dialog.java:231)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.app.Activity.showDialog(Activity.java:2407)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at net.sosiouxme.WhenDidI.custom.AlarmEditActivity.openNewAlarmDialog(AlarmEditActivity.java:179)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at net.sosiouxme.WhenDidI.custom.AlarmEditActivity.onOptionsItemSelected(AlarmEditActivity.java:188)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at net.sosiouxme.WhenDidI.activity.TrackerEdit.onOptionsItemSelected(TrackerEdit.java:146)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.app.Activity.onMenuItemSelected(Activity.java:2085)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:820)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:813)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:519)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.View.onTouchEvent(View.java:3828)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.widget.TextView.onTouchEvent(TextView.java:6291)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.View.dispatchTouchEvent(View.java:3368)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1525)
08-21 00:42:48.595: ERROR/AndroidRuntime(880):     at android.os.Handler.dispatchMessage(Handler.java:99)`

上述内容可能有助于了解TrackerEdit是否继承自AlarmEditActivity。 TrackerEdit有一个OptionsMenu,当我从该菜单中选择一个选项时,它会尝试通过openDialog打开一个对话框。堆栈跟踪似乎表明它在有机会创建对话框之前崩溃。任何帮助将不胜感激。

编辑 - 是的,好吧,有些代码可能非常有用,不过我认为可能很清楚我的类是以net.sosiouxme开头并收到onOptionsItemSelected并调用Activity的部分。的ShowDialog()。但好的,这是来自AlarmEditActivity的那些位:

/* ***************************** event handling *************************** */

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.new_alarm:
        openNewAlarmDialog();
        break;
    }
    return super.onOptionsItemSelected(item);
}

/*
 * Open dialog for user to create a new alarm
 */
public void openNewAlarmDialog() {
    Alarm alarm = new Alarm(mTracker.getId());
    alarm.setIvalWeeks(1);
    mAlarmToEdit = alarm;
    mViewOfAlarmToEdit = null;
    showDialog(ALARM_DIALOG); // which is 0
}

代码没有做任何非常有趣的事情。选择菜单项后,它会尝试显示一个对话框。代码永远不会达到创建对话框的程度,所以我甚至没有机会搞砸了。关于该活动唯一有趣的事情是,真正的活动是TrackerEdit,它扩展了AlarmEditActivity,因此正在执行的代码位于活动的父级中。

1 个答案:

答案 0 :(得分:8)

艾德伯内特帮助我弄清楚发生了什么。我在这里为其他有类似问题的人录制。

当我调试通过时,我错了;实际上正在创建对话框,并且在显示对话框期间我收到了错误。原因是当我创建对话框时,我将getApplicationContext()作为Context传递给构造函数。

dialog = new AlarmEditDialog(getApplicationContext());

相反,我需要传递活动,这本身就是一个上下文。

dialog = new AlarmEditDialog(this); // the Activity

显然存在差异: - )