Android 指定的孩子已经有父母

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3674413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:17:32  来源:igfitidea点击:

the specified child already has a parent

androidandroid-alertdialog

提问by Kandha

I created the AlertDialogusing the builder. It shows when we call the show()method. I have the cancelbutton in that dialog. I can cancel that dialog by clicking the cancel button. My problem is once I cancelled displaying the dialog, I can't show the dialog again. It throws an exception like:

我创建了AlertDialog使用构建器。它显示我们何时调用该show()方法。我在那个对话框中有取消按钮。我可以通过单击取消按钮来取消该对话框。我的问题是一旦我取消显示对话框,就无法再次显示对话框。它抛出一个异常,如:

09-09 12:25:06.441: ERROR/AndroidRuntime(2244): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1970)
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at android.view.ViewGroup.addView(ViewGroup.java:1865)
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at android.view.ViewGroup.addView(ViewGroup.java:1845)
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at com.android.internal.app.AlertController.setupView(AlertController.java:364)
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at com.android.internal.app.AlertController.installContent(AlertController.java:205)
09-09 12:25:06.441: ERROR/AndroidRuntime(2244):     at android.app.AlertDialog.onCreate(AlertDialog.java:251)

回答by Konstantin Burov

Don't show the same dialog, create a new one.

不要显示相同的对话框,创建一个新对话框。

This is happening because you are trying to re-use the dialog which was already created (Probably at onCreate)and used once. There is no issue in reusing a dialog but as in the question the the specified child (the view) already has a parent (the dialog).You could either continue by removing the parent or you can create a new parent like :-

发生这种情况是因为您正在尝试重新使用已经创建(可能在onCreate)并使用过一次的对话框。重用对话框没有问题,但在问题中,指定的子项(视图)已经有父项(对话框)。您可以继续删除父项,也可以创建新的父项,例如:-

alertDialog=new AlertDialog(Context);
alertDialog.setView(yourView);
alertDialog.show();

回答by AZ_

remove the previous dialog before adding new one. If you continue adding new dialog each time this will stay in your memory and your app will consume more battery.

在添加新对话框之前删除上一个对话框。如果您每次都继续添加新对话框,这将保留在您的记忆中,您的应用程序将消耗更多电池。

call remove view or removeAllViews() on layout in which you are adding your dialog.

在添加对话框的布局上调用 remove view 或 removeAllViews() 。

回答by E Player Plus

You must be doing this:

你一定是这样做的:

AlertDialog.setView(yourView);

You can over come this error by:

您可以通过以下方式克服此错误:

if (yourView.getParent() == null) {
    AlertDialog.setView(yourView);
} else {
    yourView = null; //set it to null
    // now initialized yourView and its component again
    AlertDialog.setView(yourView);
}

回答by Simon

Move all the code of the builder outside of the onCreateDialogmethod.

将构建器的所有代码移到onCreateDialog方法之外。

For instance here is the Android Dialogs guide updated :

例如,这里是更新的 Android 对话框指南:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_fire_missiles)
    .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int id) {
             // Send the positive button event back to the host activity
             mListener.onDialogPositiveClick(NoticeDialogFragment.this);
         }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            // Send the negative button event back to the host activity
            mListener.onDialogNegativeClick(NoticeDialogFragment.this);
        }
    });

final Dialog dialog = builder.create();

DialogFragment fragment = new DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
        return dialog;
    }
};
fragment.show();

// and later ...
fragment.show();