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


i got error of specified child has parent. must call removeview() on child's parent first below case, see if offer help! thanks!

there scrollview of 4 tablelayout, e.g. ex_1_list, ex_2_list , on. there 4 kinds of groups , show different categories of data in database respective tablelayout, if database blank, inflate in each of 4 tablelayouts view showing there no data in each category.

first database size, if size=0, mean no data in exercises database, goto else loop, follows:

mainactivity class

    int = exercises.size();        (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 start activity componentinfo{com.examples.abc/com.example.abc.exercises_mainactivity}: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2110) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2135) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread.access$700(activitythread.java:140) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread$h.handlemessage(activitythread.java:1237) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.os.handler.dispatchmessage(handler.java:99) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.os.looper.loop(looper.java:137) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread.main(activitythread.java:4921) 07-16 01:48:44.503: e/androidruntime(13094):    @ java.lang.reflect.method.invokenative(native method) 07-16 01:48:44.503: e/androidruntime(13094):    @ java.lang.reflect.method.invoke(method.java:511) 07-16 01:48:44.503: e/androidruntime(13094):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1027) 07-16 01:48:44.503: e/androidruntime(13094):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:794) 07-16 01:48:44.503: e/androidruntime(13094):    @ dalvik.system.nativestart.main(native method) 07-16 01:48:44.503: e/androidruntime(13094): caused by: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. 07-16 01:48:44.503: e/androidruntime(13094):    @ android.view.viewgroup.addviewinner(viewgroup.java:3620) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.view.viewgroup.addview(viewgroup.java:3491) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.widget.tablelayout.addview(tablelayout.java:425) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.view.viewgroup.addview(viewgroup.java:3436) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.widget.tablelayout.addview(tablelayout.java:407) 07-16 01:48:44.503: e/androidruntime(13094):    @ com.examples.abc.exercises_mainactivity.inflate_all_ex_data(exercises_mainactivity.java:224) 07-16 01:48:44.503: e/androidruntime(13094):    @ com. examples.abc.exercises_mainactivity.oncreate(exercises_mainactivity.java:144) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activity.performcreate(activity.java:5206) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1094) 07-16 01:48:44.503: e/androidruntime(13094):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2074) 07-16 01:48:44.503: e/androidruntime(13094):    ... 11 more 

question:

how specified child has parent. must call removeview() on child's parent first solved?

thanks!

you trying add same view different parents. must inflate same view every time want add different parent.

i can suggest make loop 4 limit(you know have 4 tablelayouts), inflate same view each time , @ end add view 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 last line must figure out how know tablelayout corresponds //to index, maybe name ¿?  } 

Comments