3

ビューを動的に追加したい。

LinearLayout mDescriptionLayout = (LinearLayout)findViewById(R.id.descriptionLayout); 

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate (R.layout.bonus_info_item, null,false);
TextView counter = (TextView) view.findViewById(R.id.counter);
TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
String [] s = {"","A","B", "C","D"};

for (int i = 1; i < 5; i++){
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}

これは私のもので、この膨張したビューを( )main.xmlに追加したいLinearLayout@id/descriptionLayout

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
                android:id="@+id/days"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"   
                android:textSize="12sp"/>

        <LinearLayout
                android:id="@+id/descriptionLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/bonusCardTitleText"
                android:orientation="vertical">

        </LinearLayout>
        </RelativeLayout>
    </ScrollView>

ここでビューを動的に追加できず、常にこのエラーが発生するという問題が何であるかがわかりません。

Caused by: 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:3339)
        at android.view.ViewGroup.addView(ViewGroup.java:3210)
        at android.view.ViewGroup.addView(ViewGroup.java:3155)
        at android.view.ViewGroup.addView(ViewGroup.java:3131)
4

2 に答える 2

1

エラーが言うように、

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

子を親に追加しようとしていますが、View子には既に親があります( の最初の繰り返しの後for loop)。View内部を宣言して初期化するか、毎回for loop呼び出すことで修正できます。remove()これをそのように複数回追加しようとしているかどうかはわかりませんlayoutが、そうであれば試してみてください

for (int i = 1; i < 5; i++)
{
    View view = inflater.inflate (R.layout.bonus_info_item, null,false);
    TextView counter = (TextView) view.findViewById(R.id.counter);
    TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}

別の方法は電話するmDescriptionLayout.removeView(view);ことですが、あなたの状況ではこれをしたくないと思います. Viewを介して各反復で新しいを初期化するとloop、問題が解決するはずです。

于 2013-07-11T14:56:36.167 に答える
1

に同じTextViewオブジェクトを複数回追加していLinearLayoutます。TextViewアイテムごとに新しいインスタンスを作成する必要があります。

for (int i = 1; i < 5; i++){
    View view = inflater.inflate (R.layout.bonus_info_item, null,false);
    TextView counter = (TextView) view.findViewById(R.id.counter);
    TextView descriptionText = (TextView) view.findViewById(R.id.descriptionText);
    counter.setText(String.valueOf(i));
    descriptionText.setText(s[i]);
    mDescriptionLayout.addView(view);
}
于 2013-07-11T14:56:46.150 に答える