AlertDialog.show() 抛出 RuntimeException

4

当我想在活动开始时显示一个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: 主线程 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: 无法添加窗口 -- token null 不是应用程序 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: 无法添加窗口 -- token 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): 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

你的StartActivity.java文件中第82行是哪一行? - Mohit Deshpande
1个回答

18

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


谢谢,伙计!!!我想了解更多信息,我们什么时候可以使用这个和getApplicationContext()方法?你能帮帮我吗…… - amity
2
@amity:这个想法是应用程序上下文比活动上下文更通用。一个“Activity”(它扩展了“Context”)添加了一些关于它所显示的窗口的引用。我不知道是否有其他类似对话框的例子,但可能会有。 - bigstones

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接