ビューを動的に追加したい。
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)