javiersantos / MaterialStyledDialogs

A library that shows a beautiful and customizable Material-based dialog with header. API 14+ required.
Apache License 2.0
1.17k stars 155 forks source link

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

Open fparrav opened 6 years ago

fparrav commented 6 years ago
Details
Reproduction Steps

1.I add a CustomView that contains 2 ImageButton

  1. Then inflate the layout in the activity 3.Then I discard the dialogue that was invoked from a floacting action button
  2. 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 commented 6 years ago

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

slimTouati commented 5 years ago

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