Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I got an error of specified child already has a parent. You must call removeView() on the child's parent first for the below case, see if you could offer any help! Thanks!

There is a ScrollView of 4 TableLayout, e.g. ex_1_list, ex_2_list and so on. There are 4 kinds of groups and I would like to show different categories of data in the database to the respective TableLayout, but if database is blank, then it could inflate in each of the 4 Tablelayouts a view showing there are no data in each category.

First to get the database size, if size=0, mean no data in the exercises database, it will goto the else loop, as follows:

MainActivity class

    int i = exercises.size();   
    for (int j = 0; j < i; j++) 
    {
        Inflate_All_Ex_Data(j);
    }       
    if (i==0)
    {
        Inflate_All_Ex_Data (0);
    }

private void Inflate_All_Ex_Data (int index) 
{   
    ...
    else
    {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View newTagView = inflater.inflate(R.layout.exercises_info, null);

        Button exercise_id = (Button) newTagView.findViewById(R.id.exercise_id);
        Button exercise_group = (Button) newTagView.findViewById(R.id.exercise_group);
        Button exercise_name = (Button) newTagView.findViewById(R.id.exercise_name);
        Button exercise_count = (Button) newTagView.findViewById(R.id.exercise_count);

        exercise_group.setText("");
        exercise_id.setText("");
        exercise_name.setText("No data!");
        exercise_calory.setText("");            

        table_list.removeAllViews();

        ex_1_List.addView(newTagView, index);
        ex_2_List.addView(newTagView, index); //Line224
        ex_3_List.addView(newTagView, index);
        ex_4_List.addView(newTagView, index);
    }   

xml file:

    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/Table2"
        android:layout_below="@+id/view1" >

        <LinearLayout
            android:id="@+id/table_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/add"
            android:layout_margin="2dp"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/ex_1_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/blue_btn" />

            <TableLayout
                android:id="@+id/ex_2s_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/green_btn" />

            <TableLayout
                android:id="@+id/ex_3_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/yellow_btn" />

            <TableLayout
                android:id="@+id/ex_4_List"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@drawable/pink_btn" />

        </LinearLayout>
    </ScrollView>

Logcat:

07-16 01:48:44.503: E/AndroidRuntime(13094): FATAL EXCEPTION: main
07-16 01:48:44.503: E/AndroidRuntime(13094): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.abc/com.example.abc.Exercises_MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.os.Looper.loop(Looper.java:137)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread.main(ActivityThread.java:4921)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at dalvik.system.NativeStart.main(Native Method)
07-16 01:48:44.503: E/AndroidRuntime(13094): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3620)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.view.ViewGroup.addView(ViewGroup.java:3491)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.widget.TableLayout.addView(TableLayout.java:425)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.view.ViewGroup.addView(ViewGroup.java:3436)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.widget.TableLayout.addView(TableLayout.java:407)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at com.examples.abc.Exercises_MainActivity.Inflate_All_Ex_Data(Exercises_MainActivity.java:224)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at com. examples.abc.Exercises_MainActivity.onCreate(Exercises_MainActivity.java:144)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.Activity.performCreate(Activity.java:5206)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-16 01:48:44.503: E/AndroidRuntime(13094):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
07-16 01:48:44.503: E/AndroidRuntime(13094):    ... 11 more

Question:

How could this specified child already has a parent. You must call removeView() on the child's parent first be solved?

Thanks!

like image 902
pearmak Avatar asked Jul 15 '13 18:07

pearmak


1 Answers

You are trying to add the same view to different parents. You must inflate the same view every time you want to add it to a different parent.

I can suggest you to make a for loop with four as limit(you know you only have four tableLayouts), inflate the same view each time and at the end add that view to the TableLayout.

for(int i=0;i<4;i++){

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newTagView = inflater.inflate(R.layout.exercises_info, null);
...

currentTableLayout.addView(newTagView);
//In this last line you must figure it out how to know what tableLayout corresponds
//to the i index, maybe with the name ¿?

}
like image 57
Julio_oa Avatar answered Oct 19 '22 20:10

Julio_oa