Android 在显示 ProgressDialog 时出现错误

发布于 2024-11-19 20:03:25 字数 4135 浏览 3 评论 0原文

我有一个活动,我想在其中显示 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 将其作为对话框中的内容有什么问题?

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(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. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睫毛上残留的泪 2024-11-26 20:03:25

试试这个。

ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "", 
                        "Loading. Please wait...", true);  

谢谢。

simply try this.

ProgressDialog dialog = ProgressDialog.show(yourActivity.this, "", 
                        "Loading. Please wait...", true);  

Thanks.

帅冕 2024-11-26 20:03:25

此错误消息在添加内容之前必须调用 requestFeature() 意味着您正在调用对话框上的方法,而在添加内容后不允许您调用该方法。

我会移动这些行:

dialog.setCancelable(true);
dialog.setTitle(getResources().getString(R.string.dialog_download_title));

以便它们位于此行之前:

dialog.setContentView(tw);

setTitle 是最有可能出现错误的候选者。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文