[英]Android getting Error while show ProgressDialog
I have an Activity where I want to show a ProgressDialog.我有一个要显示 ProgressDialog 的活动。 I override onCreateDialog in my Activity.我在我的活动中覆盖 onCreateDialog。 Everytime the dialog returns in onCreateDialog I get an force-close, saying:每次对话框在 onCreateDialog 中返回时,我都会强制关闭,说:
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at com.android.internal.app.AlertController.installContent(AlertController.java:199)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.ProgressDialog.onCreate(ProgressDialog.java:176)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.Activity.createDialog(Activity.java:886)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.Activity.showDialog(Activity.java:2557)
07-13 13:10:11.449: ERROR/AndroidRuntime(8720): at android.app.Activity.showDialog(Activity.java:2524)
the onCreateDialog(int id) looks like this: onCreateDialog(int id) 看起来像这样:
protected Dialog onCreateDialog(int id){
switch (id) {
case DOWNLOAD_DIALOG:
ProgressDialog dialog = new ProgressDialog(ListMapActivity.this);
dialog.setTitle(getResources().getString(R.string.dialog_download_title));
dialog.setCancelable(true);
dialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if ((mTask.getStatus().equals(AsyncTask.Status.RUNNING)
|| mTask.getStatus().equals(AsyncTask.Status.PENDING))
&& !mTask.isCancelled()){
mTask.cancel(true);
}
Toast.makeText(getApplicationContext(), R.string.dialog_download_cancel, Toast.LENGTH_LONG).show();
}
});
TextView tw = new TextView(ListMapActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tw.setLayoutParams(params);
tw.setText(getResources().getString(R.string.dialog_download_text));
dialog.setContentView(tw);
return dialog;
default:
return null;
}
}
Note: I tried ProgressDialog.Show(context, title, message) to execute directly instead of showDialog(), with this result:注意:我尝试直接执行 ProgressDialog.Show(context, title, message) 而不是 showDialog(),结果如下:
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.view.ViewRoot.setView(ViewRoot.java:531)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.app.Dialog.show(Dialog.java:241)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.app.ProgressDialog.show(ProgressDialog.java:107)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.app.ProgressDialog.show(ProgressDialog.java:90)
07-13 13:03:42.910: ERROR/AndroidRuntime(8444): at android.app.ProgressDialog.show(ProgressDialog.java:85)
I assume I forget something to initialize.我假设我忘记了一些要初始化的东西。 I don't even know where this requestFeature() is been called.我什至不知道这个 requestFeature() 是在哪里调用的。 Any Idea is welcome欢迎任何想法
UPDATE: I played around a little bit and found out, that everything works again, if I delete these lines:更新:我玩了一下发现,如果我删除这些行,一切都会再次运行:
LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tw.setLayoutParams(params);
tw.setText(getResources().getString(R.string.dialog_download_text));
dialog.setContentView(tw);
So I'm lost on how to set the Content of this View.所以我不知道如何设置这个视图的内容。 It doesn't help, when I create the Main-design in onCreate of my Activity as a member and put this into ProgressDialog.当我作为成员在我的 Activity 的 onCreate 中创建主设计并将其放入 ProgressDialog 时,它没有帮助。 I simply don't get the problem.我根本不明白这个问题。 What is wrong putting a TextView on-the-fly to put it as Content from my Dialog?将 TextView 动态放置以将其作为我的对话框中的内容有什么问题?
simply try this.试试这个。
ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "",
"Loading. Please wait...", true);
Thanks.谢谢。
This error message requestFeature() must be called before adding content
means that you are invoking a method on the dialog, that you aren't allowed to invoke after you had added content.此错误消息requestFeature() must be called before adding content
调用,这意味着您正在调用对话框上的方法,在添加内容后不允许调用该方法。
I would move these lines:我会移动这些行:
dialog.setCancelable(true);
dialog.setTitle(getResources().getString(R.string.dialog_download_title));
So that they are before this line:所以他们在这条线之前:
dialog.setContentView(tw);
setTitle is your most likely candidate for the error. setTitle 是您最有可能出现该错误的候选者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.