0

我正在尝试创建一个在创建活动时弹出的对话框。这是我创建对话框的方法:

public Dialog createDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());

    builder.setMessage("Order Information");
    builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            //Create order object in here
            Toast.makeText(getApplication(), "order created!", Toast.LENGTH_SHORT).show();
        }
    });
    return builder.create();
}

然后我在我的 onCreate 方法中调用它,如下所示:

Dialog d = createDialog();
    d.show();

这是我得到的错误:

java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.waitron5/com.example.waitron5.MainActivity}: android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序

原因:android.view.WindowManager$BadTokenException: Unable to add window -- token null 不适用于应用程序

这是我的日志猫错误:

01-21 16:10:45.533: E/AndroidRuntime(9228): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.waitron5/com.example.waitron5.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.os.Looper.loop(Looper.java:137)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at java.lang.reflect.Method.invokeNative(Native Method)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at java.lang.reflect.Method.invoke(Method.java:511)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at dalvik.system.NativeStart.main(Native Method)
01-21 16:10:45.533: E/AndroidRuntime(9228): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:571)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:246)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Dialog.show(Dialog.java:281)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at com.example.waitron5.MainActivity.onCreate(MainActivity.java:52)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Activity.performCreate(Activity.java:5104)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-21 16:10:45.533: E/AndroidRuntime(9228):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-21 16:10:45.533: E/AndroidRuntime(9228):     ... 11 more

知道我做错了什么,我知道可能是 DialogBox 没有获得对创建它的活动的引用。我正在考虑在静态变量中存储对活动的引用并以这种方式调用它,但我读到由于内存泄漏,不建议这样做。

非常感谢任何建议!

4

1 回答 1

1

只是改变

AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());

AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this);
于 2013-01-21T16:15:47.713 回答