我正在尝试创建一个在创建活动时弹出的对话框。这是我创建对话框的方法:
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:无法添加窗口 - 令牌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没有获得对它的创建活动的引用。我想在静态变量中存储对活动的引用并以这种方式调用它,但我读到由于内存泄漏而不建议这样做。
非常感谢任何建议!
答案 0 :(得分:1)
只需更改
AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());
到
AlertDialog.Builder builder = new AlertDialog.Builder(YourClassName.this);