the specified child already has a parent

后端 未结 4 1722
梦如初夏
梦如初夏 2020-12-20 18:17

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 cance

4条回答
  •  自闭症患者
    2020-12-20 18:29

    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();
    

提交回复
热议问题