【问题标题】:Adding muliple rows to a TableLayout将多行添加到 TableLayout
【发布时间】:2019-09-13 14:59:29
【问题描述】:

当只添加第一行时一切正常,但在我添加第二行之后,它就失败了。

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);

日志猫

进程:com.hazofim.thescouts,PID:17992 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.hazofim.thescouts/com.hazofim.thescouts.attendes}:java.lang.IllegalStateException:指定的孩子已经有了父母。您必须首先在孩子的父母上调用 removeView()。在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.-wrap11(Unknown Source:0) 在 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) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 在 com.android.internal.os .ZygoteInit.main(ZygoteInit.java:767) 引起:java.lang.IllegalStateException:指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。在 android.view.ViewGroup.addViewInner(ViewGroup.java:4915) 在 android.view.ViewGroup.addView(ViewGroup.java:4746) 在 android.widget.TableLayout.addView(TableLayout.java:427) 在 android.view。 ViewGroup.addView(ViewGroup.java:4686) 在 android.widget.TableLayout.addView(TableLayout.java:409) 在 android.view.ViewGroup.addView(ViewGroup.java:4659) 在 android.widget.TableLayout.addView(TableLayout .java:400) 在 com.hazofim.thescouts.attendes.onCreate(attendes.java:48) 在 android.app.Activity.performCreate(Activity.java:6975) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1213) 在 android.app.ActivityThread.-wrap11(Unknown Source:0) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 在 android。 app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 在 android.os.Handler.dispatchMessage(Handler.java:105) 在 android.os.Looper.loop(Looper.java:164) 在 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)

【问题讨论】:

  • 拖最后一句 tableLayout.addView(row,0); tableLayout.addView(row,1);当我添加第二行时,应用程序失败,只有第一行一切顺利

标签: java android


【解决方案1】:

为了防止出现该错误消息:

指定的孩子已经有一个父母。

需要有两个TableRow(“指定的孩子”)实例,无论它们的重复内容如何:

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);
}

方法.addView() 甚至不需要传递index(默认情况下,它会将项目添加到末尾)。

【讨论】:

  • 我的问题是当我添加应用程序下降的第二行时
  • @אילייקורן-יב7 您没有添加任何第二行,而是同一行(指同一个对象)两次...如果您要添加崩溃的堆栈跟踪,我可以告诉更多关于它的信息。
  • 我需要将同一行添加两次
  • @אילייקורן-יב7 需要查看堆栈跟踪以了解它发生的原因。您可以尝试使用 TableRow 的两个单独实例,这样就会传递两个不同的引用。
  • 什么是堆栈跟踪?以及我添加的任何行的下降
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多