0

In fragment :I'm trying to add view but getting error Exception IllegalStateException .

Following solution i have tried:

1) solution one

2) Call removeView() on the child's parent first

3) The specified child already has a parent. You must call removeView() on the child's parent first (Android)

After tried all above solution still getting same error.

Here is my code:

Fragment.java:

 View kidInfoView = getLayoutInflater().inflate(R.layout.include_kid_info_layout,null,false);

        if (onCollapsingToolbarLayout().getParent() !=null) {
           ViewGroup parent = (ViewGroup) onCollapsingToolbarLayout().getParent();
           parent.removeView(kidInfoView);
            onCollapsingToolbarLayout().addView(kidInfoView); <-- getting error here
        }

@Override
    public CollapsingToolbarLayout onCollapsingToolbarLayout() {
        collapsingToolbarLayout.setVisibility(View.VISIBLE);
        return collapsingToolbarLayout;
    }

What am i doing wrong ? please suggest.

chand mohd
  • 2,363
  • 1
  • 14
  • 27

1 Answers1

0

If you want to add some view in collapsing toolbar you need to write code as follows.
View idInfoView = getLayoutInflater().inflate(R.layout.include_kid_info_layout, onCollapsingToolbarLayout(), true);

i don't know what you are doing in if condition.

your are getting parent of collepsingtoolbar and from this parent you are calling remove view on kidInfoView(as first child)(which is inflated two statement above and not added to parent).
after that you are adding kidInfoView to collapsingtoolbar (but kindInfoView Has no matching layout params.)

Amit Goswami
  • 314
  • 2
  • 11