"指定的孩子已经有父母"添加新的TableLayout

时间:2015-08-09 10:25:21

标签: java android android-linearlayout tablelayout

当我尝试将程序化创建的TableLayout添加到如下所示的LinearLayout时,我收到此错误:

08-09 10:20:26.288: E/AndroidRuntime(1878): Process: de.bc.cologne, PID: 1878
08-09 10:20:26.288: E/AndroidRuntime(1878): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bc.cologne/de.bc.cologne.activity.ProtokollActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread.access$800(ActivityThread.java:151)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.os.Looper.loop(Looper.java:135)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread.main(ActivityThread.java:5257)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at java.lang.reflect.Method.invoke(Native Method)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at java.lang.reflect.Method.invoke(Method.java:372)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-09 10:20:26.288: E/AndroidRuntime(1878): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3936)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.view.ViewGroup.addView(ViewGroup.java:3786)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.widget.TableLayout.addView(TableLayout.java:429)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.view.ViewGroup.addView(ViewGroup.java:3727)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.widget.TableLayout.addView(TableLayout.java:411)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.view.ViewGroup.addView(ViewGroup.java:3700)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.widget.TableLayout.addView(TableLayout.java:402)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at de.bc.cologne.activity.ProtokollActivity.onCreate(ProtokollActivity.java:76)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.Activity.performCreate(Activity.java:5990)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
08-09 10:20:26.288: E/AndroidRuntime(1878):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
08-09 10:20:26.288: E/AndroidRuntime(1878):     ... 10 more

布局 - XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="#FFFFFF"
    android:id="@+id/linearLayoutProtokoll"
    tools:context="${relativePackage}.${activityClass}" >



</LinearLayout>

活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_protokoll);

    mLinearLayoutProtokoll = (LinearLayout)findViewById(R.id.linearLayoutProtokoll);

    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setLayoutParams(tableParams);

    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(tableParams);

    mTableLayout = new TableLayout(this);
    mTableLayout.setGravity(Gravity.CENTER);
    mTableLayout.setLayoutParams(tableParams);

    mFileDataSource = new FileDataSource(this);
    List<Protokoll> lAllProtokoll = mFileDataSource.loadProtokoll();

    TableRow lTableRow = new TableRow(this);
    lTableRow.setLayoutParams(tableParams);
    for (Protokoll lProtokoll : lAllProtokoll) {
        TextView lTextViewCreated = new TextView(this);
        lTextViewCreated.setText("date");
        lTextViewCreated.setLayoutParams(rowParams);
        lTableRow.addView(lTextViewCreated);

        TextView lTextViewType = new TextView(this);
        lTextViewType.setLayoutParams(rowParams);
        if (lProtokoll.getType() == 0) {
            lTextViewType.setText("INFO");
        } else {
            lTextViewType.setText("ERROR");
        }
        lTableRow.addView(lTextViewType);

        TextView lTextViewFile = new TextView(this);
        lTextViewFile.setLayoutParams(rowParams);
        if (lProtokoll.getFile() == null) {
            lTextViewFile.setText("");
        } else {
            lTextViewFile.setText(lProtokoll.getFile());
        }
        lTableRow.addView(lTextViewFile);

        TextView lTextViewMessage = new TextView(this);
        lTextViewMessage.setLayoutParams(rowParams);
        lTextViewMessage.setText(lProtokoll.getMessage());
        lTableRow.addView(lTextViewMessage);

        mTableLayout.addView(lTableRow);
    }
    mLinearLayoutProtokoll.addView(mTableLayout);
}

我真的不知道这个问题是什么...... 你能帮帮我吗?

1 个答案:

答案 0 :(得分:4)

您正在为表格布局添加相同的表格行对象:

TableRow lTableRow = new TableRow(this);

将该代码移至“for”循环。