the specified child already has a parent

2019-01-27 22:00发布

I created the AlertDialog using the builder. It shows when we call the show() method. I have the cancel button 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:

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)

4条回答
ゆ 、 Hurt°
2楼-- · 2019-01-27 22:13

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);
}
查看更多
【Aperson】
3楼-- · 2019-01-27 22:17

Move all the code of the builder outside of the onCreateDialog method.

For instance here is the Android Dialogs guide updated :

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();
查看更多
贼婆χ
4楼-- · 2019-01-27 22:27

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.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-27 22:37

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 :-

alertDialog=new AlertDialog(Context);
alertDialog.setView(yourView);
alertDialog.show();
查看更多
登录 后发表回答