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);
                    //}


                }
            }
        });
cstokes2
  • 105
  • 1
  • 2
  • 8

2 Answers2

0

You are adding the same view object to the parent. So, you are getting IllegalStateException. If you seperate child views with "id" attribute, adding will be successful. You can give different ids to the child views via hashcode method.

  • Thanks, but im not familiar with this hashCode method? could you point to a link? @sercan – cstokes2 May 28 '13 at 14:42
  • 1
    hashCode method returns an integer value for objects. This value will be different for every object. So when you assign id to the views, you can use view.setId(view.hashCode()). You can learn hash code in Wikipedia or here : http://stackoverflow.com/questions/2427631/how-is-hashcode-calculated-in-java – Sercan Turker May 28 '13 at 14:53
  • Ive tried the hashCode method and it still throws the same error @sercan – cstokes2 May 28 '13 at 15:13
  • You must assign different id for different child views. For example, row1.setId(1); row2.setId(2); table.addView(row1); table.addView(row2); row1 and row2 can be added with this way. you can use hashcode method to be sure that ids are different. – Sercan Turker May 28 '13 at 17:22
  • thanks i wasn't creating a new text view and row after each iteration – cstokes2 May 28 '13 at 18:06
0

You are adding same TextView object to the row each time. In each iteration, create a new TextView and a TableRow.

for(int j = 0; j < resultset.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);

//}

}

Bharat Jyoti
  • 394
  • 2
  • 6