I have an Activity where I want to show a ProgressDialog. I override onCreateDialog in my Activity. Everytime the dialog returns in onCreateDialog I get an force-close, saying:
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:
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:
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(844开发者_如何学JAVA4): 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. 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. I simply don't get the problem. What is wrong putting a TextView on-the-fly to put it as Content from my Dialog?
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.
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.
精彩评论