在我的应用程序中创建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)
答案 0 :(得分:1)
您必须使用show dialog的活动参考..
使用此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;