AlertDialog.show()上的RuntimeException

时间:2011-03-19 15:12:14

标签: android alertdialog 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):致命异常:主要   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:ERROR / AndroidRuntime(15145):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在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:ERROR / 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: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):引起:android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null不适用于应用程序   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在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:ERROR / AndroidRuntime(15145):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)   03-19 16:04:33.933:ERROR / AndroidRuntime(15145):... 11更多

1 个答案:

答案 0 :(得分:17)

使用this代替getApplicationContext()。更多信息here