错误试图打开一个对话框:android.view.WindowManager $ BadTokenException [英] Error trying to open a dialog: android.view.WindowManager$BadTokenException

查看:176
本文介绍了错误试图打开一个对话框:android.view.WindowManager $ BadTokenException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从我的Andr​​oid活动打开一个对话框收到此错误。谷歌搜索没有提供任何有用的跨pretations。希望有人有一些想法是什么这个例外,甚至意味着什么?

I received this error while trying to open a dialog from my Android activity. A Google search did not provide any helpful interpretations. Hoping someone has some idea what this exception even means?

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弹出一个对话框。堆栈跟踪似乎表明,它曾经得到一个机会来创建对话框之前崩溃。任何帮助将是AP preciated。

It might be helpful in the above to know that TrackerEdit inherits from AlarmEditActivity. TrackerEdit has an OptionsMenu, and when I choose an option from that menu it attempts to bring up a dialog via openDialog. The stack trace seems to indicate that it crashes before ever getting a chance to create the dialog. Any help would be appreciated.

修改 - 是的,好吧,有些code可能是pretty帮助,不过我想这可能是pretty清楚,我的课是与网开始部分.sosiouxme并收到onOptionsItemSelected并呼吁Activity.showDialog()。但是好了,下面是那些位时,从AlarmEditActivity:

EDIT - yes, alright, some code might be pretty helpful, though I thought it might be pretty clear that my classes were the part that started with net.sosiouxme and received onOptionsItemSelected and called Activity.showDialog(). But OK, here are those bits from 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
}

在code没有做任何事情非常有趣。当选择该菜单项,它会尝试显示一个对话框。在code从来没有到一个对话框是要创建的点,所以我还没有有机会搞砸这件事。关于这就是远程有趣的活动的唯一事情是,真正的活动是延伸AlarmEditActivity TrackerEdit,所以正在执行的code是在活动的父。

The code isn't doing anything terribly interesting. When the menu item is chosen, it tries to show a dialog. The code never got to the point where a dialog was to be created, so I haven't even had the chance to mess that up. About the only thing that's remotely interesting about the activity is that the real activity is TrackerEdit which extends AlarmEditActivity, so the code being executed is in the parent of the activity.

推荐答案

埃德·伯内特帮助我弄清楚发生了什么事情。我在这里记录它为别人有类似的问题。

Ed Burnett helped me figure out what was going on. I record it here for anyone else with a similar problem.

原来,当我调试通过,我错了;实际上正在创建的对话框,这是我得到的错误对话框显示期间。其原因是,当我创建对话框,我通过getApplicationContext()来构造的上下文。

Turns out when I debugged through, I was wrong; the dialog was actually being created, and it was during the showing of the dialog that I got the error. The reason was that when I created the dialog, I passed getApplicationContext() to the constructor as the Context.

dialog = new AlarmEditDialog(getApplicationContext());

相反,我需要通过该活动,这本身就是一个上下文。

Instead I needed to pass the activity, which is itself a Context.

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

显然有区别: - )

Evidently there's a difference :-)

这篇关于错误试图打开一个对话框:android.view.WindowManager $ BadTokenException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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