3

I want to add some view dynamically.

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);
}

This is my main.xml and I want add this inflated view in 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>

I don't understand what is the problem in here that I can't add view dynamically and I always got this error.

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)
ridoy
6,3722 gold badges32 silver badges60 bronze badges
asked Jul 11, 2013 at 14:53
1
  • When you create your layout in xml, the layouts are already being created. You don't have to add them again, this will cause error. Commented Jul 11, 2013 at 14:56

2 Answers 2

1

As the error says,

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

you are trying to add a child View to a parent and the child already has a parent( after the first iteration of your for loop). Either declaring and initializing the View inside the for loop or calling remove() each time should fix it. I'm not sure if you are trying to add this layout multiple times like that but if so then you can try

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);
}

Another way would be to call mDescriptionLayout.removeView(view); but I don't think you want to do this in your situation. Initializing a new View with each iteration through the loop should solve your problem.

answered Jul 11, 2013 at 14:56
Sign up to request clarification or add additional context in comments.

Comments

1

You are adding the same TextView object more than once to the LinearLayout. You need to create a new instance of TextView for each item.

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);
}
ridoy
6,3722 gold badges32 silver badges60 bronze badges
answered Jul 11, 2013 at 14:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.