"The specified child already has a parent" 添加新的 TableLayout

"The specified child already has a parent" adding new 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.access0(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>

Activity代码:

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

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

您正在将 for 循环中的相同 table 行对象添加到 table 布局:

TableRow TableRow = new Table Row(this);

将该代码移至 "for" 循环。