gpt4 book ai didi

android - BadTokenException无法添加窗口(ProgressDialog)

转载 作者:行者123 更新时间:2023-12-02 10:55:19 26 4
gpt4 key购买 nike

在应用程序中创建ProgressDialog时,出现BadTokenException错误。我尝试阅读有关该问题的几篇文章,但我仍然无法真正弄清这是什么原因。单击按钮后,我调用对话框函数。

    protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_LOADING:
dialog = new Dialog(this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.loading);
break;


default:
dialog = null;
break;
}
return dialog;
};

上面的代码是我的进度对话框代码。我只用 showDialog(DIALOG_LOADING);来称呼它,谁能告诉我是什么原因造成的,以及如何解决这个问题?任何意见将不胜感激。
12-18 12:12:39.250: E/AndroidRuntime(7654): FATAL EXCEPTION: main
12-18 12:12:39.250: E/AndroidRuntime(7654): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4057a200 is not valid; is your activity running?
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.ViewRoot.setView(ViewRoot.java:532)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:200)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.app.Dialog.show(Dialog.java:241)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.app.Activity.showDialog(Activity.java:2566)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.app.Activity.showDialog(Activity.java:2524)
12-18 12:12:39.250: E/AndroidRuntime(7654): at com.fl.wbillboard.Web_Input$1.onClick(Web_Input.java:128)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.View.performClick(View.java:2538)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.view.View$PerformClick.run(View.java:9152)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.os.Handler.handleCallback(Handler.java:587)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.os.Looper.loop(Looper.java:130)
12-18 12:12:39.250: E/AndroidRuntime(7654): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-18 12:12:39.250: E/AndroidRuntime(7654): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 12:12:39.250: E/AndroidRuntime(7654): at java.lang.reflect.Method.invoke(Method.java:507)
12-18 12:12:39.250: E/AndroidRuntime(7654): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
12-18 12:12:39.250: E/AndroidRuntime(7654): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-18 12:12:39.250: E/AndroidRuntime(7654): at dalvik.system.NativeStart.main(Native Method)

最佳答案

您必须将 Activity 的引用用于显示对话框。

使用此new Dialog(activity_name.this, android.R.style.Theme_Translucent);代替new Dialog(this, android.R.style.Theme_Translucent);

case DIALOG_LOADING:
dialog = new Dialog(activity.this, android.R.style.Theme_Translucent);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.loading);
break;

关于android - BadTokenException无法添加窗口(ProgressDialog),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13926104/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com