在XML中初始化TableLayout并以编程方式填充它-Android(java)

在XML中初始化TableLayout并以编程方式填充它-Android(java),java,android,xml,tablelayout,Java,Android,Xml,Tablelayout,我试图用XML初始化一个TableLayout,然后以编程方式填充它。这是我的XML、java和LogCat输出 Highscores.java public class Highscores extends Activity { TableLayout table; TableRow rowHeader, row1, row2, row3, row4, row5, row6, row7, row8, row9, row10; TextView rank, percen

我试图用XML初始化一个TableLayout,然后以编程方式填充它。这是我的XML、java和LogCat输出

Highscores.java

public class Highscores extends Activity {

    TableLayout table;
    TableRow rowHeader, row1, row2, row3, row4, row5, row6, row7, row8, row9, row10;
    TextView rank, percentage, score;
    Button btn1;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.highscoresmain);

        TableLayout table = (TableLayout)findViewById(R.id.tableLayout);

        TextView rank = (TextView)findViewById(R.id.rank);
        rank.setText("RANK");
        TextView percentage = (TextView)findViewById(R.id.percentage);
        percentage.setText("PERCENTAGE");
        TextView score = (TextView)findViewById(R.id.score);
        score.setText("SCORE");

        TableRow rowHeader = (TableRow)findViewById(R.id.rowHeader);

        rowHeader.addView(rank);  //Line 39
        rowHeader.addView(percentage);
        rowHeader.addView(score);

        Button btn1 = (Button)findViewById(R.id.homeBtn);

        table.addView(rowHeader, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        btn1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent(Highscores.this, MainMenu.class);
                startActivity(intent);
            }
        });
    }
}
highscoresmain.xml

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:padding="5dp">

    <TableRow 
        android:layout_height="wrap_content"
        android:id="@+id/rowHeader">
        <TextView
            android:id="@+id/rank"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/percentage"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/score"
            android:layout_height="wrap_content" />
    </TableRow>
      </TableLayout>
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:padding="5dp">


   </TableLayout>

我想要的是一个简单的3列表格,第一行标题是“排名”、“百分比”和“分数”。稍后,我将从SQLite数据库中再提取10行,但稍后我将处理这些行。

您不需要以下代码:

TableRow rowHeader = (TableRow)findViewById(R.id.rowHeader);

rowHeader.addView(rank);  //Line 39
rowHeader.addView(percentage);
rowHeader.addView(score);
table.addView(行标题,新的TableLayout.LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));


因为它们已经在布局中定义。

答案在您的
logcat
中:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.test/com.example.test.Highscores}: java.lang.IllegalStateException:指定的子级已具有 父母亲必须首先对子级的父级调用removeView()

TextView
已经在
TableRow
中,如果您想以编程方式填充它,只需删除
TableLayout
的所有子项,在code中创建它们,并像刚才那样将它们添加到
活动的
onCreate


祝你好运

您的xml文件将被编辑

我试图用XML初始化一个TableLayout,然后以编程方式填充它。这是我的XML、java和LogCat输出

highscoresmain.xml

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:padding="5dp">

    <TableRow 
        android:layout_height="wrap_content"
        android:id="@+id/rowHeader">
        <TextView
            android:id="@+id/rank"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/percentage"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/score"
            android:layout_height="wrap_content" />
    </TableRow>
      </TableLayout>
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:padding="5dp">


   </TableLayout>

当我调用TableLayout的addview时,它会产生异常。为什么?您要添加哪个视图以及如何添加?我想添加包含两个文本视图的表行。您会遇到哪些异常?