2016-07-29 47 views
2

我想关闭/关闭警报对话框,但当我点击按钮valider我有这个错误:我不知道哪些视图给我这个问题,甚至有没有方法去除查看我的意见“布局”或“v关闭孩子的父母的alertDialog Android removeView()第一个

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; 
     } 
    }); 
+0

问题是,你正试图关闭一个没有显示的对话框..你为什么要做show()。dismiss()?它没有意义 – br00

+0

我没有b.dismiss(),我只得到b.show()。dismiss() – Euphor08

回答

2

更改此

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

谢谢,对于每个人都有同样的问题,不要忘记声明alertDialog最终像在答案**最后AlertDialog alertDialog = b.show()**; – Euphor08

-1

相反的第是

b.show().dismiss(); 

试试这个

b.dismiss(); 

当你调用方法Show()两次,你得到这个错误。

+0

没有b.dismiss()方法,只有这个b.show()。dismiss() ); – Euphor08

0

试试下面的代码

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

      d.dismiss(); 
     } 
相关问题