【问题标题】:Can't add TextView to a new TableRow (The specified child already has a parent)无法将 TextView 添加到新的 TableRow(指定的子级已经有父级)
【发布时间】:2014-04-28 00:27:45
【问题描述】:

我试图将新的 TableRows 添加到 TableLayout,但我什至无法将 TextView 添加到新的 TableRow 对象。但是总是有同样的错误:

04-25 12:11:42.329: E/AndroidRuntime(26636): java.lang.IllegalStateException:指定的孩子已经有一个 父母。您必须先在孩子的父母上调用 removeView()。

这是主要代码:

public static class PlaceholderFragment extends Fragment {

protected TableLayout mTableLayout;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_results,
            container, false);

    mTableLayout = (TableLayout) rootView
            .findViewById(R.id.results_table);
    fullFillTable();

    return rootView;
}

private void fullFillTable() {
    TextView labelPosition = new TextView(getActivity());
    TableRow row = new TableRow(getActivity());
    labelPosition.setText("Pos");
    row.addView(labelPosition); //The error is here
    mTableLayout.addView(row);
   }
}

这里是fragment.xml

<TableLayout
        android:id="@+id/results_table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:stretchColumns="*" >

        <TableRow
            android:id="@+id/results_titles_row"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#F7F7F7" >

            <TextView
                android:id="@+id/results_col1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/results_col1_title" />

            <TextView
                android:id="@+id/results_col2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/results_col2_title" />

            <TextView
                android:id="@+id/results_col3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/results_col3_title" />

            <TextView
                android:id="@+id/results_col4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/results_col4_title" />
         </TableRow>
</TableLayout>

有人知道怎么解决吗?

谢谢

【问题讨论】:

    标签: android tablelayout tablerow


    【解决方案1】:

    您将labelPosition 添加到两个不同的视图中,这是错误的:

    row.addView(labelPosition);
    mTableLayout.addView(labelPosition);
    

    相反,您应该将TextView 添加到TableRow 并将TablewRow 添加到TableLayout,如下所示:

    row.addView(labelPosition);
    mTableLayout.addView(row);
    

    【讨论】:

    • 谢谢,我改变了这个。但问题出在第一个 addView()
    • @marc_aragones,你确定吗?你用固定的代码重建项目了吗?
    • 是的,当然。 logcat 说错误在以下行: row.addView(labelPosition);
    【解决方案2】:

    虽然不是我想要的方式,但我已经能够解决它。

    创建一个row_layout.xml 文件:

    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/col1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello world" />
    </TableRow>
    

    然后,给它充气:

    private void fullFillTable() {
        TableRow row;
        row = (TableRow) View.inflate(getActivity(), R.layout.row_layout, null);
        ((TextView)row.findViewById(R.id.col1)).setText("Pos");
        mTableLayout.addView(row);
    }
    

    但是,我想知道为什么它以前不起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-23
      • 2015-11-01
      • 2020-03-21
      • 2020-11-04
      • 1970-01-01
      • 2017-02-23
      • 2014-07-09
      • 1970-01-01
      相关资源
      最近更新 更多