我想关闭/关闭警报对话框,但是当我单击按钮valider时,出现了一个错误:我不知道哪个视图会给我带来问题,即使在我的视图“layout”或“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)

我不太清楚
 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;
        }
    });

最佳答案

改变这个

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


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

然后改变这个
b.show().dismiss();


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

关于android - 首先在 child 的 parent 上关闭alertDialog Android removeView(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38655633/

10-11 22:23
查看更多