fergerm
fergerm

Reputation: 241

The specified child already has a parent. You must call removeChild() on child's parent when populating a Table Row

Hello I have the following error when trying to add Views into a TableRow. I get the error message: "The specified child already has a parent. You must call removeChild() on child's parent" when the second View is added. Here is the code I'm using:

TableLayout table2 = new TableLayout(this);
table2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 
            TableLayout.LayoutParams.WRAP_CONTENT));

for(int count = 0; count < films.size() ; count++){

        TableRow tableRow = new TableRow(this);

        Film film = films.get(count);
        count++;

        Film film2 = new Film();
        if(count != films.size()){
            film2 = films.get(count);
            count++;
        }

        tableRow.addView(flim.getPhoto());

        if(!film2.getName().contentEquals("")){
            //tableRow.addView(film2.getPhoto());
        }

        table2.addView(tableRow); // Add the new row to our tableLayout
    }
    LinearLayout l2 = (LinearLayout)findViewById(R.id.root);
    l2.addView(table2);
}

When I try to add the 2° film, I get the following exception:

?:??: W/?(?): FATAL EXCEPTION: main
?:??: W/?(?): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pipoca/com.vista.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
?:??: W/?(?):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
?:??: W/?(?):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
?:??: W/?(?):   at android.app.ActivityThread.access$600(ActivityThread.java:130)
?:??: W/?(?):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
?:??: W/?(?):   at android.os.Handler.dispatchMessage(Handler.java:99)
?:??: W/?(?):   at android.os.Looper.loop(Looper.java:137)
?:??: W/?(?):   at android.app.ActivityThread.main(ActivityThread.java:4745)
?:??: W/?(?):   at java.lang.reflect.Method.invokeNative(Native Method)
?:??: W/?(?):   at java.lang.reflect.Method.invoke(Method.java:511)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
?:??: W/?(?):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
?:??: W/?(?):   at dalvik.system.NativeStart.main(Native Method)
?:??: W/?(?): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
?:??: W/?(?):   at android.view.ViewGroup.addViewInner(ViewGroup.java:3378)
?:??: W/?(?):   at android.view.ViewGroup.addView(ViewGroup.java:3249)
?:??: W/?(?):   at android.view.ViewGroup.addView(ViewGroup.java:3194)
?:??: W/?(?):   at android.view.ViewGroup.addView(ViewGroup.java:3170)
?:??: W/?(?):   at com.vista.MainActivity.armarTabla(MainActivity.java:264)
?:??: W/?(?):   at com.vista.MainActivity.onTabSelected(MainActivity.java:200)
?:??: W/?(?):   at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:344)
?:??: W/?(?):   at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
?:??: W/?(?):   at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:356)
?:??: W/?(?):   at com.android.internal.app.ActionBarImpl.setNavigationMode(ActionBarImpl.java:1158)
?:??: W/?(?):   at com.actionbarsherlock.internal.app.ActionBarWrapper.setNavigationMode(ActionBarWrapper.java:204)
?:??: W/?(?):   at com.vista.MainActivity.showTabsNav(MainActivity.java:190)
?:??: W/?(?):   at com.vista.MainActivity.onCreate(MainActivity.java:63)
?:??: W/?(?):   at android.app.Activity.performCreate(Activity.java:5008)
?:??: W/?(?):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
?:??: W/?(?):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
?:??: W/?(?):   ... 11 more
?:??: W/?(?): [ 02-28 07:34:23.878   417: 2107 W/ActivityManager ]
?:??: W/?(?):   Force finishing activity com.pipoca/com.vista.MainActivity
?:??: W/?(?): [ 02-28 07:34:24.378   417:  430 W/ActivityManager ]
?:??: W/?(?): Activity pause timeout for ActivityRecord{415eaff0 com.pipoca/com.vista.MainActivity}
?:??: W/?(?): [ 02-28 07:34:24.388   417:  430 D/WindowManager ]

Could anyone please tell what am I doing wrong? Thanks in advance

Upvotes: 1

Views: 868

Answers (1)

Goddchen
Goddchen

Reputation: 4458

The exception indicates that the View (i suppose it's an ImageView) returned by your getPhoto() method was already added to a parent before.

Simply use your IDE to find all usages of getPhoto() to identify where this happens.

Upvotes: 1

Related Questions