我想动态添加一些视图。
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)