Reputation: 1
When adding just the first row everything go well, but after I've added the second row, it fails.
pupils=db.getAllPupilsPupil();
events=db.getAllEvents();
TableLayout tableLayout =(TableLayout) findViewById(R.id.table);
bubbleSort(events);
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView tv;
tv = new TextView(this);
tv.setText("events ");
row.addView(tv);
for(int i=0;i<pupils.size();i++) {
tv = new TextView(this);
String name=pupils.get(i).getName()+" ";
tv.setText(name);
tv.setTextColor(getResources().getColor(R.color.colorBlack));
row.addView(tv);
}
tableLayout.addView(row,0);
tableLayout.addView(row,1);
the logcat
Process: com.hazofim.thescouts, PID: 17992 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hazofim.thescouts/com.hazofim.thescouts.attendes}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. at android.view.ViewGroup.addViewInner(ViewGroup.java:4915) at android.view.ViewGroup.addView(ViewGroup.java:4746) at android.widget.TableLayout.addView(TableLayout.java:427) at android.view.ViewGroup.addView(ViewGroup.java:4686) at android.widget.TableLayout.addView(TableLayout.java:409) at android.view.ViewGroup.addView(ViewGroup.java:4659) at android.widget.TableLayout.addView(TableLayout.java:400) at com.hazofim.thescouts.attendes.onCreate(attendes.java:48) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Upvotes: 0
Views: 50
Reputation: 76689
In order to prevent that error message:
The specified child already has a parent.
There need to be two instances of TableRow
(the "specified child"), no matter their duplicate content:
for(int i=0; i < pupils.size(); i++) {
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
TextView tv = new TextView(this);
tv.setText(pupils.get(i).getName());
tv.setTextColor(getResources().getColor(R.color.colorBlack));
row1.addView(tv);
row2.addView(tv);
tableLayout.addView(row1);
tableLayout.addView(row2);
}
method .addView() doesn't even need an index
passed (it adds items to the end, by default).
Upvotes: 1