以编程方式添加表格行时,我关闭了一个强制关闭?

时间:2012-02-24 06:50:48

标签: android

我正在以编程方式进行表格布局,当我将表格行添加到布局应用程序时显示强制关闭可以任何正文帮助我,以下是我的代码

TableLayout tl = (TableLayout) findViewById(R.id.maintable);
    TableRow tr = new TableRow(this);
    TextView labelTV = new TextView(this);
    TextView valueTV = new TextView(this);
    // Go through each item in the array
    for (int current = 0; current < numProvinces; current++)
    {
        // Create a TableRow and give it an ID

        tr.setId(100+current);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));   

        // Create a TextView to house the name of the province

        labelTV.setId(200+current);
        labelTV.setText(provinces[current]);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);

        // Create a TextView to house the value of the after-tax income

        valueTV.setId(current);
        valueTV.setText("$0");
        valueTV.setTextColor(Color.WHITE);
        valueTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(valueTV);

        // Add the TableRow to the TableLayout
        tl.addView(tr,new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        setContentView(tl);   
    }

4 个答案:

答案 0 :(得分:0)

已经存在的视图可能是您正在获得的例外,

TableRow tr = new TableRow(this);

将上面的行添加到for循环中..

这种情况正在发生,因为您试图一次又一次地添加相同的TableRow ..

<强>更新 试试这个

tl.addView(tr);

而不是

tl.addView(tr,new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

答案 1 :(得分:0)

TableLayout tl = (TableLayout) findViewById(R.id.maintable);
TableRow[] tr;
TextView[] labelTV;
TextView[] valueTV;

tr = new TableRow[numProvinces];
labelTV = new TextView[numProvinces];
valueTV= new TextView[numProvinces];

tl.removeAllViews();
tl.refreshdrawablestate();

for (int current = 0; current < numProvinces; current++)
{      
   tr[current] = new TableRow(this);
   labelTV[current] = new TextView(this);
   labelTV[current] = new TextView(this);

// Create a TableRow and give it an ID

    tr.setId(100+current);
    tr.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));   

    // Create a TextView to house the name of the province

    labelTV.setId(200+current);
    labelTV.setText(provinces[current]);
    labelTV.setTextColor(Color.WHITE);
    labelTV.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(labelTV);

    // Create a TextView to house the value of the after-tax income

    valueTV.setId(current);
    valueTV.setText("$0");
    valueTV.setTextColor(Color.WHITE);
    valueTV.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    tr.addView(valueTV);

    // Add the TableRow to the TableLayout
    tl.addView(tr,new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

}
  setContentView(tl); 

答案 2 :(得分:0)

TableLayout tl = (TableLayout) findViewById(R.id.maintable);

for (int current = 0; current < numProvinces; current++)
{

    TableRow tr = new TableRow(this);
    TextView labelTV = new TextView(this);
    TextView valueTV = new TextView(this);   


    /// Do your stuff   


        tr.addView(labelTV );
        tr.addView(valueTV );

        tbl.addView(tr);    


}

试试这个答案

答案 3 :(得分:0)

我使用xml文件中的更改测试了您的代码并且它可以正常工作。我发布了我的代码,以便您可以与自己的代码进行比较。 我复制你的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //I add some data for test here:

    int numProvinces = 5;
    String[] provinces =new String[]{"1","2","3","4","5"};

    //below is the same with your code:

    TableLayout tl = (TableLayout) findViewById(R.id.maintable);
    // Go through each item in the array
    for (int current = 0; current < numProvinces; current++)
    {
        TableRow tr = new TableRow(this);
        TextView labelTV = new TextView(this);
        TextView valueTV = new TextView(this);
        // Create a TableRow and give it an ID

        tr.setId(100+current);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));   

        // Create a TextView to house the name of the province

        labelTV.setId(200+current);
        labelTV.setText(provinces[current]);
        labelTV.setTextColor(Color.WHITE);
        labelTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(labelTV);

        // Create a TextView to house the value of the after-tax income

        valueTV.setId(current);
        valueTV.setText("$0");
        valueTV.setTextColor(Color.WHITE);
        valueTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        tr.addView(valueTV);

        // Add the TableRow to the TableLayout
        tl.addView(tr,new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        setContentView(tl);   
    }

}

这是我的主要布局(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/maintable"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"

/>