user1172113
user1172113

Reputation: 1

"specified child already has a parent" error

I have a page that will display a weeks worth of scheduled games for a selected sport. The selected sport and week is stored within a XML file that is opened at the the start of the page. When I click on the next week link, a new week date value is stored to this xml file and then the page is reloaded.Any games for this next week should now be displayed. These games are displayed in a tablelayout where I programmatically create the rows as needed in the .java page. Here is a simplified sample of the code.

TableLayout tlGames = (TableLayout)findViewById(R.id.table_games);

for (int i = 0; i < gameCount; i++) 
{

TableRow trGameHomeTeam = new TableRow(this);

TextView tvHomeTeamName = new TextView(this);

tvHomeTeamName.setText("Home Team Name Here");

tvHomeTeamName.setLayoutParams(new LayoutParam (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

TextView tvHomeTeamScore = new TextView(this);

tvHomeTeamScore.setText("Home Team Score Here");

tvHomeTeamScore.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

trGameHomeTeam.addView(tvHomeTeamName);

trGameHomeTeam.addView(tvHomeTeamScore);

tlGames.addView(trGameHomeTeam,new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

}           

When I initially run my app, the games for the current date are listed properly. Then once I click on the next week link, it should repload this page and display the games for this new week. Instead, I get an error message of "The specified child already has a parent. You must call removeView() on the child's parent first." I added a "tlGames.removeAllViews();" immediately after the TableLayout tlGames line but this did not correct the error.

The only places were I have the "addView" is for creating table rows or the textviews in the table rows.

Can anyone tell me what I am doing wrong?

Thanks

Upvotes: 0

Views: 3399

Answers (3)

Gangnus
Gangnus

Reputation: 24464

You have corrected already some errors, as I see. Correct two more:

    tvHomeTeamName.setLayoutParams(new TableRow.LayoutParams (LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

...

    tvHomeTeamScore.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

With these corrections it runs at me.

Upvotes: 0

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

First you have to tell at which line you get that error. Or put stack-trace.

Now, You got that error message just because you alredy have the view and you are again going to add another view on the place of that same view.

So Remove the added view with the specific name first before adding the another view.

Ex. tl.removeView(blablabla); then add the view.

Or

If you want to load the Specific view of loop condition then you can also try this.

Create another xml as you want to execute base on the loop condition. Ex. say as child.xml

Then add that layout to your main layout based on the loop condition. You can also access with the data of that layout in that loop.

Have look of this Loop that i have done.

    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
            LinearLayout EmpItem = (LinearLayout)LayoutInflater.from        (getApplicationContext()).inflate(R.layout.child, null); 
   // to access with the data use this

    ((TextView)EmpItem.findViewById(R.id.taxcodeTextView)).setText(TAXCODE);


    // Finaly add that loopLayout to your layout to your main layout like this
    myLinearLayout.addView(EmpItem); 
}

It will surly help you as i have tested it.

If not then let me inform.

Or

If Still you want to do something with your own code then understand this example

It will surly going to help you.

Enjoy. :))

Upvotes: 0

frikkenator
frikkenator

Reputation: 241

My guess is that with the reloading of the page to display the games for this week, you use a layout inflater to inflate the details layout?

The layout inflater is the only place I've seen this error occur, if that is the case, pass a third argument of false to the inflate method, as it will attempt to inflate the root of your view otherwise, thus throwing this exception back at you.

i.e.

inflater.inflate(R.layout.profile, container, false);

Upvotes: 4

Related Questions