Dismiss alertDialog Android removeView() on the ch

2019-07-13 03:00发布

问题:

I want to close/dismiss the alert Dialog but when I click in button valider I had this error : I don't know which views give me the problem, even there's no method remove View in my views "layout" or "v"

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.view.ViewGroup.addView(ViewGroup.java:3391)
at com.android.internal.app.AlertController.setupView(AlertController.java:413)
at com.android.internal.app.AlertController.installContent(AlertController.java:241)
 at android.app.AlertDialog.onCreate(AlertDialog.java:337)
 at android.app.Dialog.dispatchOnCreate(Dialog.java:361)
 at android.app.Dialog.show(Dialog.java:262)
  at android.app.AlertDialog$Builder.show(AlertDialog.java:951)
  at com.surtymar.application.Fragments.FormContactFragment$6$1.onClick(FormContactFragment.java:503)

I don't know exactly

 v.findViewById(R.id.btn_author).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                LayoutInflater inflater = (LayoutInflater) LayoutInflater.from(getActivity());
                final View layout = inflater.inflate(R.layout.accomplice_layout_solution, null);
                layout.setBackgroundColor(colors.getBackMixColor(colors.getForeground_color(), 0.30f));
                ListView lv = (ListView) layout.findViewById(R.id.listForm);

                adapterAgent = new MyFormAgentAdapter((MainActivity) getActivity(), finalfieldsAgent, realm, colors, R.layout.form_row_item);

                lv.setAdapter(adapterAgent);
                final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
                b.setView(layout);
                b.show();
                AllFilds.addAll(finalFields);

                layout.findViewById(R.id.btn_valide_form).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        v.findViewById(R.id.numberPinContainerAuthor).setVisibility(View.VISIBLE);
                        TextView textView = (TextView) v.findViewById(R.id.numberOfCatsAuthor);

                        int numbr = 0;
                        if(AllFilds.size() > 0)
                            numbr++;
                         textView.setText(""+numbr);

                        b.show().dismiss(); // the error is here


                    }
                });

            }
            return false;
        }
    });

回答1:

Change this

final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
            b.setView(layout);
            b.show();

to

final AlertDialog.Builder b = new AlertDialog.Builder(getActivity());
    b.setView(layout);
    final AlertDialog alertDialog = b.show();

and change this

b.show().dismiss();

to

if (alertDialog.isShowing())
        alertDialog.dismiss();


回答2:

try following code

        Dialog d  = b.show(); // here you got the dialog object when u showing you dialog first 
        if (d!=null &&d.isShowing()){

            d.dismiss();
        }


回答3:

Instead of this

b.show().dismiss();

Try this

b.dismiss();

As you are calling method show() twice, you are getting that error.