android - Using two layout in xml but getting an error: must call removeView() on the child's parent first -
i have searched the issue couldn't find solution according situation. using 2 table layouts in xml layout , adding dynamically created rows these layouts. each row contain further views. here code
button b[]; edittext ed[]; tablelayout layouttable,layoutbutton; linearlayout llayouttable,llayoutbutton; int value,answer; tablerow row[],rowb[]; textview tv[],tv1[],tv2[],tv3[]; @suppresslint("newapi") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.table_game_layout); intent intent = getintent(); value = intent.getintextra("keyint", 0); layouttable = (tablelayout)findviewbyid(r.id.layouttable); layoutbutton = (tablelayout)findviewbyid(r.id.layoutbutton); layouttable.removeallviews(); layoutbutton.removeallviews(); tv = new textview[17]; row = new tablerow[17]; tv1 = new textview[17]; tv2 = new textview[17]; tv3 = new textview[17]; ed = new edittext[17]; b = new button[17]; rowb = new tablerow[5]; layoutparams params = new layoutparams(android.support.v4.view.viewpager.layoutparams.wrap_content, android.support.v4.view.viewpager.layoutparams.match_parent); layouttable.setlayoutparams(params); layoutbutton.setlayoutparams(params); android.widget.tablerow.layoutparams params1 = new tablerow.layoutparams(0,layoutparams.wrap_content,1f); for(int i=1; i<=16; i++){ tv[i] = new textview(this); tv1[i] = new textview(this); tv2[i] = new textview(this); tv3[i] = new textview(this); ed[i] = new edittext(this); row[i] = new tablerow(this); row[i].removeallviews(); tv[i].setlayoutparams(params1); tv1[i].setlayoutparams(params1); tv2[i].setlayoutparams(params1); ed[i].setlayoutparams(params1); tv3[i].setlayoutparams(params1); tv[i].settextsize(30); tv1[i].settextsize(30); tv2[i].settextsize(30); tv3[i].settextsize(30); ed[i].settextsize(30); tv[i].settext(""+value); tv1[i].settext("x"); tv2[i].settext(""+i); tv3[i].settext("="); row[i].addview(tv[i]); row[i].addview(tv1[i]); row[i].addview(tv2[i]); row[i].addview(tv3[i]); row[i].addview(ed[i]); answer= i*value; b[i] = new button(this); b[i].settext(""+answer); layouttable.addview(row[i]); } for(int i=1; i<=4; i++){ int j=1; rowb[i] = new tablerow(this); rowb[i].removeallviews(); rowb[i].addview(b[j]); rowb[i].addview(b[j+1]); rowb[i].addview(b[j+2]); rowb[i].addview(b[j+3]); j+=4; layoutbutton.addview(rowb[i]); }
}
heren xml layout
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:orientation="vertical" > <scrollview android:layout_width="match_parent" android:layout_height="wrap_content" > <linearlayout android:id="@+id/llayouttable" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <tablelayout android:id="@+id/layouttable" android:layout_width="match_parent" android:layout_height="wrap_content" > </tablelayout> </linearlayout> </scrollview> </linearlayout> <linearlayout android:id="@+id/llayoutbutton" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" > <tablelayout android:id="@+id/layoutbutton" android:layout_width="match_parent" android:layout_height="wrap_content" > </tablelayout> </linearlayout>
here logcat errors
07-14 14:57:53.382: e/androidruntime(5538): fatal exception: main 07-14 14:57:53.382: e/androidruntime(5538): java.lang.runtimeexception: unable start activity componentinfo{com.example.tablegame/com.example.tablegame.mainactivity}: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread.access$600(activitythread.java:141) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 07-14 14:57:53.382: e/androidruntime(5538): @ android.os.handler.dispatchmessage(handler.java:99) 07-14 14:57:53.382: e/androidruntime(5538): @ android.os.looper.loop(looper.java:137) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread.main(activitythread.java:5041) 07-14 14:57:53.382: e/androidruntime(5538): @ java.lang.reflect.method.invokenative(native method) 07-14 14:57:53.382: e/androidruntime(5538): @ java.lang.reflect.method.invoke(method.java:511) 07-14 14:57:53.382: e/androidruntime(5538): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-14 14:57:53.382: e/androidruntime(5538): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-14 14:57:53.382: e/androidruntime(5538): @ dalvik.system.nativestart.main(native method) 07-14 14:57:53.382: e/androidruntime(5538): caused by: java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first. 07-14 14:57:53.382: e/androidruntime(5538): @ android.view.viewgroup.addviewinner(viewgroup.java:3339) 07-14 14:57:53.382: e/androidruntime(5538): @ android.view.viewgroup.addview(viewgroup.java:3210) 07-14 14:57:53.382: e/androidruntime(5538): @ android.view.viewgroup.addview(viewgroup.java:3155) 07-14 14:57:53.382: e/androidruntime(5538): @ android.view.viewgroup.addview(viewgroup.java:3131) 07-14 14:57:53.382: e/androidruntime(5538): @ com.example.tablegame.mainactivity.oncreate(mainactivity.java:105) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activity.performcreate(activity.java:5104) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 07-14 14:57:53.382: e/androidruntime(5538): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 07-14 14:57:53.382: e/androidruntime(5538): ... 11 more
Comments
Post a Comment