0

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?

4

1 に答える 1

0

問題は、でレイアウトを準備する方法が原因だと思いますprepareThemes()

あなたは言及しませんでしたが、あなたの'layout / widget_configure.xml'は次のような構造を定義していると思います:?

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

次に、これを膨らませprepareThemes()て3つのサブレイアウトを取得します。しかし、現時点では、それらはすでに周囲のレイアウト(私は「ルート」と呼んでいます)の子として登録されています。エラーが示すように、ビューインスタンスは1つの親にのみ追加できます

各LinearLayoutを独自のxmlファイルに保存してから、3回膨らませることができます。これらの保存されたレイアウトは、子に1回だけ追加できます。

何をしたいのかわかりませんが、準備するのではなく、パーツを含む3つの異なるレイアウトを用意してから、必要なレイアウトをその場でlayout.itemrow作成switch casegetChildView()て膨らませた方がよいと思います。 。バインドされていないレイアウトをprepareThemesに保存する場合でも、複数のグループがあるとすぐに、事前に保存されたレイアウトをsndグループの子に追加するときに同じエラーが発生します。

これがお役に立てば幸いです。

于 2010-08-08T23:20:06.587 に答える