views:

55

answers:

1

Hi guys, I want to inflate a childView of ExpandableChildView component.

Code:

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

        View v = convertView;
        LinearLayout linearOpt = themesOptions.get(childPosition);

        if(v == null) {
            v = mInflater.inflate(R.layout.itemrow, null);
        }

        LinearLayout ll = (LinearLayout) v.findViewById(R.id.root);
        ll.addView(linearOpt);
        return v;

    }

Where linearOpt is a vector that contains a lot of LinearLayout objects that I have instantiated.

    private void prepareThemes(){
        View v = mInflater.inflate(R.layout.widget_configure, null);
        LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
        LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
        LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);

        themesOptions.add(theme);
        themesOptions.add(color);
        themesOptions.add(trans);

    }

This is R.layout.itemrow xml:

But I received this error:

07-18 10:48:49.740: ERROR/AndroidRuntime(2738): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

How I can resolve this issue?

A: 

I think the problem is caused by the manner you prepare the layouts in prepareThemes().

You didn't mention, but I assume your 'layout/widget_configure.xml' defines a structure like this:?

<LinearLayout android:id="@+id/root">
    <LinearLayout android:id="@+id/themeLayout> <!-- ... --> </LinearLayout>
    <LinearLayout android:id="@+id/colorLayout> <!-- ... --> </LinearLayout>
    <LinearLayout android:id="@+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>

Then you inflate this in prepareThemes() to get the 3 sub layouts. But at this moment they are already registered as children of the surrounding layout (which I called "root"). As the error implies a view instance can be added to just one parent.

You could store each LinearLayout in its own xml file and then inflate 3 times. Those stored layouts then could be add exactly once to a child.

I'm not sure what you want to do, but I would guess, it would be better - instead of preparing - to just have 3 different layouts that include the part from layout.itemrow and then just make a switch case in getChildView() and inflate the needed layout on the fly. Because even when you store a not bind layout in prepareThemes: As soon as you have more then one group you would get the same error when adding a prestored layout to the child of the snd group.

I hope this helps.

sunadorer