我在制作警报对话框时遇到困难,其代码在此处:
public void setp2(View v){
p2 = (TextView)findViewById(R.id.p2);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
alertDialogBuilder.setTitle("Please enter player 2 name");
alertDialogBuilder.setMessage("MESSAGE");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
每当我运行它时崩溃logcat看起来像:
04-14 06:09:36.282: E/Trace(25945): error opening trace file: No such file or directory (2)
04-14 06:09:37.822: E/AndroidRuntime(25945): FATAL EXCEPTION: main
04-14 06:09:37.822: E/AndroidRuntime(25945): java.lang.IllegalStateException: Could not execute method of the activity
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$1.onClick(View.java:3595)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View.performClick(View.java:4088)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$PerformClick.run(View.java:16984)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Handler.handleCallback(Handler.java:615)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.os.Looper.loop(Looper.java:137)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 06:09:37.822: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-14 06:09:37.822: E/AndroidRuntime(25945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 06:09:37.822: E/AndroidRuntime(25945): at dalvik.system.NativeStart.main(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: java.lang.reflect.InvocationTargetException
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945): at java.lang.reflect.Method.invoke(Method.java:511)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.View$1.onClick(View.java:3590)
04-14 06:09:37.822: E/AndroidRuntime(25945): ... 11 more
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
04-14 06:09:37.822: E/AndroidRuntime(25945): at android.app.Dialog.show(Dialog.java:277)
04-14 06:09:37.822: E/AndroidRuntime(25945): at uk.co.mikecoombes.oxo.MainActivity.setp2(MainActivity.java:46)
04-14 06:09:37.822: E/AndroidRuntime(25945): ... 14 more
我已经好好看了好几遍,似乎无法找到它出错的地方,想知道是否有人可以提供帮助?
答案 0 :(得分:5)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
上下文看起来很糟糕,有时getApplicationContext()
会返回奇怪的结果...尝试更改此内容:
new AlertDialog.Builder(getApplicationContext());
要:
new AlertDialog.Builder(MainActivity.this);
通过链接
中的commonsware检查答案