1

我在制作警报对话框时遇到了困难,它的代码在这里:

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

看了好几遍都没有发现哪里出错了,请问有大神帮忙吗?

4

1 回答 1

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 检查答案

何时调用活动上下文或应用程序上下文?

于 2013-04-14T05:21:29.787 回答