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 回答