2

I'm trying to show a PopupWindow from a Fragment but it not works. I've got the error:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My code is:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    buttton = (Button)rootView.findViewById(R.id.my_button);
    button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
              LayoutInflater layoutInflater (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
              View popupView = layoutInflater.inflate(R.layout.popup_window, null, false);
              PopupWindow popupWindow = new PopupWindow(popupView, 450, 640);
              popupWindow.showAsDropDown(v);
         }
    });

I can't understand what's the "specified child" from the error. How can I solve this problem? Thanks

Niccolò Passolunghi
  • 5,966
  • 2
  • 25
  • 34
  • Sorry let me rephrase which view is this error occurring on? the rootview or the popupview? – Xjasz Mar 10 '15 at 17:30
  • You're using that same layout somewhere else and you didn't use parentview.removeView("the view that uses the popuplayout); In order to use that layout again you need to remove it from whatever else is using it. – Xjasz Mar 10 '15 at 17:34
  • The R.layout.popup_window is used only in this piece of code and nowhere else. The error occurs on popupView – Niccolò Passolunghi Mar 10 '15 at 17:47
  • @nicopasso Running into the same issue, have you found a solution ? – MikeOscarEcho Mar 16 '16 at 19:08
  • http://stackoverflow.com/questions/13559353/how-to-solve-for-viewpager-the-specified-child-already-has-a-parent-you-must – Bruce Mar 21 '16 at 06:26

1 Answers1

-1

Maybe you should verify whether popupWindow is showing,when it's showing,you sholud dismiss it then show.

 PopupWindow popupWindow = new PopupWindow(popupView, 450, 640);

 if(popupWindow.isShowing()){
    popupWindow.dismiss();
}
  popupWindow.showAsDropDown(v);
fabelYu
  • 41
  • 1