0

I am trying to add my query results to a table row inside a table layout. I keep getting java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. When i call:

((ViewGroup)table.getParent()).removeView(table);

I get an null pointer error. Any help on this would be great. Here`s the full code.

runOnUiThread(new Runnable() {


            public void run() {


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

                int count = response.getPropertyCount();
                System.out.println(count);
                for(int i = 0; i < count; i++)
                {
                    resultset.add(response.getProperty(i));
                }

                //TextView tv = new TextView(this);
                for(int j = 0; j < resultset.size(); j++)
                {
                    //if(resultset.get(j).toString() == "resFName")
                    //{

                        tv.setText(resultset.get(j).toString());
                        //((ViewGroup)row.getParent()).removeView(row);
                        row.addView(tv);
                        //((ViewGroup)table.getParent()).removeView(table);
                        table.addView(row);
                    //}


                }
            }
        });
4

2 回答 2

0

您每次都将相同的 TextView 对象添加到该行。在每次迭代中,创建一个新的 TextView 和一个 TableRow。

for(int j = 0; j < 结果集.size(); j++) {

 //if(resultset.get(j).toString() == "resFName")
 //{

 TextView tv = new TextView(context);

 tv.setText(resultset.get(j).toString());
 //((ViewGroup)row.getParent()).removeView(row);

 TableRow row = new TableRow(context);

 row.addView(tv);
 //((ViewGroup)table.getParent()).removeView(table);
 table.addView(row);

//}

}

于 2013-05-28T17:36:29.490 回答
0

您正在向父级添加相同的视图对象。所以,你得到了 IllegalStateException。如果您使用“id”属性分隔子视图,则添加将成功。您可以通过 hashcode 方法为子视图赋予不同的 id。

于 2013-05-28T14:38:29.933 回答