簡體   English   中英

首先在孩子的父母上調用removeView

[英]Call removeView on the child's parent first

我需要向表中動態添加表行。 在每行中必須有4個TextView。當我運行程序時,出現錯誤“您必須先在孩子的父級上調用removeView”。 我該怎么辦? 這是我的代碼和我的xml:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

 View rootView = inflater.inflate(R.layout.post_lineup_sending_layout, 
         container, false);

    tableLayout =(TableLayout)rootView.findViewById(R.id.postsending_table_layout);

    ownPlayer = (TextView) rootView.findViewById(R.id.own_player);
    pointsOwnPlayer = (TextView) rootView.findViewById(R.id.own_player_points);
    enemyPlayer = (TextView) rootView.findViewById(R.id.other_player);
    pointsEnemyPlayer = (TextView) rootView.findViewById(R.id.other_player_points);


    for(int i=0; i<4; i++){
        TableRow tr = new TableRow(CurrentContext.getContext());
        ownPlayer.setText("Ciccio");
        pointsOwnPlayer.setText(""+6);
        enemyPlayer.setText("Avversario");
        pointsEnemyPlayer.setText(""+10);
        tr.addView(ownPlayer);
        tr.addView(pointsOwnPlayer);
        tr.addView(enemyPlayer);
        tr.addView(pointsEnemyPlayer);
        tableLayout.addView(tr);
    }

    return rootView;
}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout 
        android:id="@+id/postsending_table_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

       <TableRow >

        <TextView
            android:id="@+id/own_player"
            android:text="CALCIATORE_0"
            android:padding="3dip"
            android:layout_width="125dp" />

        <TextView
            android:id="@+id/own_player_points"
            android:text="6"
            android:gravity="center"
            android:layout_width="30dp" />

        <TextView
            android:id="@+id/other_player"
            android:gravity="center"
            android:text="CALCIATORE_1"
            android:layout_width="125dp" />

        <TextView
            android:id="@+id/other_player_points"
            android:text="10"
            android:gravity="right"
            android:layout_width="30dp" />
    </TableRow> 

    </TableLayout>


</LinearLayout>

您在不同的行中多次添加了ownPlayerpointsOwnPlayerenemyPlayerpointsEnemyPlayer 但是一個視圖只能有一個父級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM