android – AlertDialog.show()上的RuntimeException

当我想在活动开始时显示alertdialog时,我不断收到异常.
我想不通为什么?我真的很感激解释.

代码:

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: Unable to start activity ComponentInfo{de.droidgroup.THMInfo/de.droidgroup.THMInfo.Activities.StartActivity}: android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at 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): at android.os.Looper.loop(Looper.java:123)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at 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: ERROR/AndroidRuntime(15145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at 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 — token null is not for an application
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): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.Dialog.show(Dialog.java:241)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at de.droidgroup.THMInfo.Activities.StartActivity.onCreate(StartActivity.java:82)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-19 16:04:33.933: ERROR/AndroidRuntime(15145): … 11 more

解决方法:

使用它而不是getApplicationContext().更多信息here.

上一篇:java – Runtim Exception扩展Exception和自定义异常从Exception扩展为什么后一个是编译时异常而另一个不是?


下一篇:Java- RuntimeException-无法编译的源代码 – 错误的树类型