4

当我想在活动开始时显示警报对话框时,我不断收到异常。我想不通为什么?我真的很感激解释。

编码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    if (!isInternetConnectionAvailable()) {
        AlertDialog alert = new AlertDialog.Builder(getApplicationContext()).create();
        alert.setMessage("TEST DIALOG!!!");
        alert.show();
    }
...

03-19 16:04:33.933: ERROR/AndroidRuntime(15145): FATAL EXCEPTION: main 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): java.lang.RuntimeException: 无法启动活动 ComponentInfo{de. droidgroup.THMInfo/de.droidgroup.THMInfo.Activities.StartActivity}: android.view.WindowManager$BadTokenException: 无法添加窗口 -- 令牌 null 不适用于应用程序 03-19 16:04:33.933: ERROR/AndroidRuntime(15145 ): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 03- 19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.ActivityThread.access$2300(ActivityThread.java:125) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app .ActivityThread$H。handleMessage(ActivityThread.java:2033) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 16:04:33.933: ERROR/ AndroidRuntime(15145): 在 android.os.Looper.loop(Looper.java:123) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.ActivityThread.main(ActivityThread.java:4627 ) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at java. lang.reflect.Method.invoke(Method.java:521) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858 ) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 03-19 16:04:33.933:ERROR/AndroidRuntime(15145): at dalvik.system.NativeStart.main(Native Method) 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- 令牌 null 不适用于应用程序 03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.view.ViewRoot.setView(ViewRoot.java:509) 03-19 16:04:33.933: ERROR /AndroidRuntime(15145): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java: 91) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.Dialog.show(Dialog.java:241) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在de.droidgroup.THMInfo.Activities.StartActivity.onCreate(StartActivity.java:82) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread .java:2627) 03-19 16:04:33.933: 错误/AndroidRuntime(15145): ... 11 更多

4

1 回答 1

17

使用this而不是getApplicationContext(). 更多信息在这里

于 2011-03-19T15:26:09.157 回答