MaterialStyledDialogs icon indicating copy to clipboard operation
MaterialStyledDialogs copied to clipboard

Application stops when loading a dialog with custom view for the second time

Open fparrav opened this issue 6 years ago • 2 comments

  • [X] I have verified there are no duplicate active or recent bugs, questions, or requests.
  • [X] I have verified that I am using the latest version of MaterialStyledDialogs.
  • [X] I have given my issue a non-generic title.
  • [X] I have read over the README and Wiki (before asking questions on how to do something).
Details
  • MaterialStyledDialogs version: 2.1
  • Device OS version: 7.1.1
  • Device Manufacturer: HDM
  • Device Name: NOKIA2
Reproduction Steps

1.I add a CustomView that contains 2 ImageButton 2. Then inflate the layout in the activity 3.Then I discard the dialogue that was invoked from a floacting action button 4. I'm invoking the dialogue again

Expected Result

Show the dialog box again

Actual Result

Application crashes with the message: java.lang.IllegalStateException: The specified child already has a parent.You must call removeView () on the child's parent first. in the logcat

fparrav avatar Mar 21 '18 17:03 fparrav

I could control the error using the Builder and reusing the MaterialStyledDialog object instead of building it every time inside the floating action button.

fparrav avatar Mar 21 '18 18:03 fparrav

Hello , i know i'm a year late , but this may help someone : View register_layout = LayoutInflater.from(MainActivity.this) .inflate(R.layout.register_layout, null); Don't put this line before your onClickListener , put it after it . like this :

    txt_create_account.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            View register_layout = LayoutInflater.from(MainActivity.this)
                    .inflate(R.layout.register_layout, null);
            MaterialStyledDialog.Builder mt =  new MaterialStyledDialog.Builder(MainActivity.this);
                    mt.setIcon(R.drawable.ic_account_circle_black_24dp)
                    .setTitle("Inscription")
                    .setDescription("Veuillez remplir tous les champs")
                    .setCustomView(register_layout)
                    .setNegativeText("Annuler")
                    .onNegative(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            dialog.dismiss();

                        }

                    })
                    .setPositiveText("Confirmer")
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                            MaterialEditText edt_register_email = (MaterialEditText) register_layout.findViewById(R.id.edt_email);
                            MaterialEditText edt_register_fname = (MaterialEditText) register_layout.findViewById(R.id.edt_fname);
                            MaterialEditText edt_register_lname = (MaterialEditText) register_layout.findViewById(R.id.edt_lname);
                            MaterialEditText edt_register_password = (MaterialEditText) register_layout.findViewById(R.id.edt_password);
                            MaterialEditText edt_register_phone = (MaterialEditText) register_layout.findViewById(R.id.edt_phone);

                            if (TextUtils.isEmpty(edt_register_email.getText().toString())) {
                                Toast.makeText(MainActivity.this, "Champ email doit etre rempli", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            if (TextUtils.isEmpty(edt_register_fname.getText().toString())) {
                                Toast.makeText(MainActivity.this, "Champ prenom doit etre rempli", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            if (TextUtils.isEmpty(edt_register_lname.getText().toString())) {
                                Toast.makeText(MainActivity.this, "Champ nom doit etre rempli", Toast.LENGTH_SHORT).show();
                                return;
                            }

                            if (TextUtils.isEmpty(edt_register_password.getText().toString())) {
                                Toast.makeText(MainActivity.this, "Champ mot de passe doit etre rempli", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if (TextUtils.isEmpty(edt_register_phone.getText().toString())) {
                                Toast.makeText(MainActivity.this, "Champ phone doit etre rempli", Toast.LENGTH_SHORT).show();
                                return;
                            }

                             /* registerUser(edt_register_email.getText().toString(),
                              edt_register_name.getText().toString(),
                              edt_register_email.getText().toString());*/
                            else {
                                registerUser(edt_register_email.getText().toString(), edt_register_fname.getText().toString(), edt_register_lname.getText().toString(), edt_register_password.getText().toString(),edt_register_phone.getText().toString());
                                // MainActivity.this.removeDialog();
                                Toast.makeText(MainActivity.this, "Votre inscription est faite avec succés", Toast.LENGTH_LONG).show();
                            }
                        }
                    }).show();
        }
    });

slimTouati avatar Mar 20 '19 14:03 slimTouati