我有一个活动,我想要显示ProgressDialog。我在我的Activity中重写了onCreateDialog。每次对话框在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)
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;
}
}
注意:我尝试直接执行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)
我认为我忘了要初始化的东西。我甚至不知道这个requestFeature()的调用位置。欢迎任何想法
更新: 我玩了一下,发现,一切都有效,如果我删除这些行:
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);
所以我迷失了如何设置此视图的内容。当我在我的Activity的onCreate中创建Main-design作为成员并将其放入ProgressDialog时,它没有帮助。我根本没有遇到问题。将TextView即时放入我的Dialog的内容有什么不对?
答案 0 :(得分:2)
试试吧。
ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "",
"Loading. Please wait...", true);
感谢。
答案 1 :(得分:0)
此错误消息requestFeature() must be called before adding content
表示您正在调用对话框中的方法,在添加内容后不允许您调用该方法。
我会移动这些行:
dialog.setCancelable(true);
dialog.setTitle(getResources().getString(R.string.dialog_download_title));
这样他们就在这一行之前:
dialog.setContentView(tw);
setTitle是你最有可能的错误候选人。