Android 1.6:“android.view.WindowManager$BadTokenException:无法添加窗口 -- 令牌 null 不适用于应用程序”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2634991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application"
提问by Dan Monego
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)
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:
我通过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;
}
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.
这有什么遗漏吗?一些问题谈到在从 中创建对话框时会出现此问题onCreate
,这是因为尚未创建活动,但这是来自菜单对象的调用,并且该appContext
变量似乎已正确填充到调试器中。
回答by Torp
Instead of :
Context appContext = this.getApplicationContext();
you should use a pointer to the activity you're in (probably this
).
而不是:
Context appContext = this.getApplicationContext();
您应该使用指向您所在活动的指针(可能this
)。
I got bitten by this today too, the annoying part is the getApplicationContext()
is verbatim from developer.android.com :(
我今天也被这个咬了,烦人的部分是getApplicationContext()
来自 developer.android.com 的逐字记录:(
回答by Samuh
You cannot display an application window/dialog through a Context that is not an Activity. Try passing a valid activity reference
您不能通过不是活动的上下文显示应用程序窗口/对话框。尝试传递有效的活动参考
回答by kenyee
Ditto on the getApplicationContext thing.
同上 getApplicationContext 的事情。
The documents on the android site says to use it, but it doesn't work...grrrrr :-P
android 网站上的文档说要使用它,但它不起作用...grrrrr :-P
Just do:
做就是了:
dialog = new Dialog(this);
"this"is usually your Activity from which you start the dialog.
“this”通常是您启动对话框的活动。
回答by Pradeep
Android documents suggests to use getApplicationContext();
Android 文档建议使用 getApplicationContext();
but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...
但它不会工作而不是在实例化 AlertDialog.Builder 或 AlertDialog 或 Dialog 时使用您当前的活动...
Ex:
前任:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
or
或者
AlertDialog.Builder builder = new AlertDialog.Builder((Your Activity).this);
回答by mahbub_siddique
Instead of getApplicationContext()
, just use ActivityName.this
而不是getApplicationContext()
,只需使用ActivityName.this
回答by T.Hawk
I had a similar issue where I had another class something like this:
我有一个类似的问题,我有另一个类是这样的:
public class Something {
MyActivity myActivity;
public Something(MyActivity myActivity) {
this.myActivity=myActivity;
}
public void someMethod() {
.
.
AlertDialog.Builder builder = new AlertDialog.Builder(myActivity);
.
AlertDialog alert = builder.create();
alert.show();
}
}
Worked fine most of the time, but sometimes it crashed with the same error. Then I realise that in MyActivity
I had...
大多数情况下工作正常,但有时会因相同的错误而崩溃。然后我意识到在MyActivity
我有...
public class MyActivity extends Activity {
public static Something something;
public void someMethod() {
if (something==null) {
something=new Something(this);
}
}
}
Because I was holding the object as static
, a second run of the code was still holding the original version of the object, and thus was still referring to the original Activity
, which no long existed.
因为我将对象保存为static
,所以第二次运行的代码仍然保存着对象的原始版本,因此仍然指的是已Activity
不存在的原始版本。
Silly stupid mistake, especially as I really didn't need to be holding the object as static
in the first place...
愚蠢的愚蠢错误,尤其是因为我真的不需要像一开始那样拿着物体static
......
回答by Satheesh
Just change it into
把它改成
AlertDialog.Builder alert_Categoryitem =
new AlertDialog.Builder(YourActivity.this);
Instead of
代替
AlertDialog.Builder alert_Categoryitem =
new AlertDialog.Builder(getApplicationContext());
回答by Anubian Noob
Another solution is to set the window type to a system dialog:
另一种解决方案是将窗口类型设置为系统对话框:
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
This requires the SYSTEM_ALERT_WINDOW
permission:
这需要以下SYSTEM_ALERT_WINDOW
权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
As the docs say:
正如文档所说:
Very few applications should use this permission; these windows are intended for system-level interaction with the user.
很少有应用程序应该使用此权限;这些窗口用于与用户进行系统级交互。
This is a solution you should only use if you require a dialog that's not attached to an activity.
这是一个只有在您需要一个未附加到活动的对话框时才应该使用的解决方案。
回答by Jinu
Don't use getApplicationContext()
on declaring dialouge
不要用于getApplicationContext()
声明对话
Always use this
or your activity.this
始终使用this
或您的activity.this
回答by mifthi
For nested dialogs this issue is very common, It works when
对于嵌套对话框,这个问题很常见,它适用于
AlertDialog.Builder mDialogBuilder = new AlertDialog.Builder(MyActivity.this);
is used instead of
被用来代替
mDialogBuilder = new AlertDialog.Builder(getApplicationContext);
this alternative.
这种选择。