0

Em getting java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

Not able to find out which view has to be removed, any help would be really helpful.

Here is the Code snippet

main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/mainLayout"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</RelativeLayout>

data.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</RelativeLayout>

Activity Code

public class ToDo extends Activity {
    /** Called when the activity is first created. */
    Button addNew;
    RelativeLayout mainLayout;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mainLayout=(RelativeLayout)findViewById(R.id.mainLayout);

        RelativeLayout rel;
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        for(int idx=0;idx<2;idx++){
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            rel = (RelativeLayout) inflater.inflate(R.layout.data,null);
            params.setMargins(0, 50, 0, 0);

             TextView fromWeb= (TextView) rel.findViewById(R.id.txt);
             fromWeb.setText("AA");

             mainLayout.addView(rel,params);
        }

    }
}
Badrinath
  • 325
  • 1
  • 7
  • 25
  • Check this link : http://stackoverflow.com/questions/10007094/java-lang-illegalstateexception-the-specified-child-already-has-a-parent – Neha.R Oct 15 '12 at 07:38

1 Answers1

0

The code below is incorrect:

TextView fromWeb= (TextView) rel.findViewById(R.id.txt);
fromWeb.setText("AA");
rel.addView(fromWeb,params); // the TextView is alredy in the rel RelativeLayout!
mainLayout.addView(rel);

because you are adding again(with the addView method) the TextView which is already in the layout file(as you previously searched for it with findViewById). Instead it should be like this:

TextView fromWeb= (TextView) rel.findViewById(R.id.txt);
fromWeb.setText("AA");
mainLayout.addView(rel);  

If you want margins for your TextView then set them in the layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="TextView" />

</RelativeLayout>
user
  • 86,916
  • 18
  • 197
  • 190
  • correct ! when i tried your edited code it is working, but the text is overlaping.even though i use params for relative Layout. check edited code – Badrinath Oct 15 '12 at 07:45
  • @Badrinath That is because you use a `RelativeLayout` in the `main.xml` file instead of a `LinearLayout` with `orientation` set to `vertical`. – user Oct 15 '12 at 07:47
  • I need to add multiple text Views one below the other so i used inflating mechanism. – Badrinath Oct 15 '12 at 07:48
  • After following the changes which u told it works like charm ! – Badrinath Oct 15 '12 at 07:50