TableLayout - 指定的子级已有父级

时间:2015-12-07 20:21:00

标签: android android-layout

我正在尝试以编程方式创建一个简单的表。但是当调用row2.addView时代码崩溃了。出现此错误:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这是我的代码:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.contnt_2);

        TableLayout tableLayout = new TableLayout(this);

        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
        tableRowParams.setMargins(1, 1, 1, 1);


        TableRow row1 = new TableRow(this);
        TableRow row2 = new TableRow(this);
        TableRow row3 = new TableRow(this);

        TextView text1 = new TextView(this);
        text1.setText("DDDDDDDDDDDDD");
        text1.setBackgroundColor(Color.GRAY);

        TextView text2 = new TextView(this);
        text2.setText("DDDDDDDDDDDDD");
        text2.setBackgroundColor(Color.GRAY);

        TextView text3 = new TextView(this);
        text3.setText("DDDDDDDDDDDDD");
        text3.setBackgroundColor(Color.GRAY);

        row1.addView(text1, tableRowParams);
        row1.addView(text2, tableRowParams);
        row1.addView(text3, tableRowParams);

        row2.addView(text1, tableRowParams);
        row2.addView(text2, tableRowParams);
        row2.addView(text3, tableRowParams);

        row3.addView(text1, tableRowParams);
        row3.addView(text2, tableRowParams);
        row3.addView(text3, tableRowParams);

        tableLayout.addView(row1);
        tableLayout.addView(row2);
        tableLayout.addView(row3);

        setContentView(tableLayout);

    }

我找不到它发生的原因。如何正确地向表中添加行?你能救我一下吗?

2 个答案:

答案 0 :(得分:1)

这是因为您使用以下代码创建了三个TextView并将其添加到第一个TableRow

row1.addView(text1, tableRowParams);
row1.addView(text2, tableRowParams);
row1.addView(text3, tableRowParams);

然后尝试将您已创建的TextView添加到第2行。

您无法将相同的TextView添加到多个TableRow,因为正如错误所述,每个TextView只能有一个父级。

您需要为每个TextView创建三个不同的TableRow

答案 1 :(得分:-1)

您的方法的主要问题:视图不能是Android中多个父级的子级。

这是一个关于如何以编程方式创建TableLayout的教程,也许您可​​以获得一些改进逻辑的想法: http://www.prandroid.com/2014/05/creating-table-layout-dynamically-in.html

我会把它粘贴在这里以防万一:

public class MainActivity extends Activity {


 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     String[] row = { "ROW1", "ROW2", "Row3", "Row4", "Row 5", "Row 6",
  "Row 7" };
        String[] column = { "COLUMN1", "COLUMN2", "COLUMN3", "COLUMN4",
  "COLUMN5", "COLUMN6" };
    int rl=row.length; int cl=column.length;

     Log.d("--", "R-Lenght--"+rl+"   "+"C-Lenght--"+cl);

  ScrollView sv = new ScrollView(this);
     TableLayout tableLayout = createTableLayout(row, column,rl, cl);
     HorizontalScrollView hsv = new HorizontalScrollView(this);

     hsv.addView(tableLayout);
  sv.addView(hsv);
  setContentView(sv);

 }

 public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
     // get row from table with rowIndex
     TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

     // get cell from row with columnIndex
     TextView textView = (TextView)tableRow.getChildAt(columnIndex);

     // make it black
     textView.setBackgroundColor(Color.BLACK);
 }
 public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){

     // get row from table with rowIndex
     TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

     // get cell from row with columnIndex
     TextView textView = (TextView)tableRow.getChildAt(columnIndex);

     textView.setText("Hello");
 }

 private TableLayout createTableLayout(String [] rv, String [] cv,int rowCount, int columnCount) {
     // 1) Create a tableLayout and its params
     TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams();
     TableLayout tableLayout = new TableLayout(this);
     tableLayout.setBackgroundColor(Color.BLACK);

     // 2) create tableRow params
     TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
     tableRowParams.setMargins(1, 1, 1, 1);
     tableRowParams.weight = 1;

     for (int i = 0; i < rowCount; i++) {
         // 3) create tableRow
         TableRow tableRow = new TableRow(this);
         tableRow.setBackgroundColor(Color.BLACK);

         for (int j= 0; j < columnCount; j++) {
             // 4) create textView
             TextView textView = new TextView(this);
           //  textView.setText(String.valueOf(j));
             textView.setBackgroundColor(Color.WHITE);
             textView.setGravity(Gravity.CENTER);

             String s1 = Integer.toString(i);
    String s2 = Integer.toString(j);
    String s3 = s1 + s2;
    int id = Integer.parseInt(s3);
    Log.d("TAG", "-___>"+id);
              if (i ==0 && j==0){
               textView.setText("0==0");
              } else if(i==0){
               Log.d("TAAG", "set Column Headers");
              textView.setText(cv[j-1]);
              }else if( j==0){
               Log.d("TAAG", "Set Row Headers");
               textView.setText(rv[i-1]);
              }else {
              textView.setText(""+id);
              // check id=23
              if(id==23){
                textView.setText("ID=23");

               }
              }

             // 5) add textView to tableRow
             tableRow.addView(textView, tableRowParams);
         }

         // 6) add tableRow to tableLayout
         tableLayout.addView(tableRow, tableLayoutParams);
     }

     return tableLayout;
 }
 }