"指定的子已具有父"错误 [英] "specified child already has a parent" error
问题描述
我有一个页面将显示价值选定运动的预定游戏一个星期。选定的运动和周被存储即在页面的开始打开一个XML文件中。当我点击下周链接,新的一周的日期值存储到该XML文件,然后在页面现在应该显示这个下周reloaded.Any游戏。这些游戏都显示在一个tablelayout根据需要将.java页面,在这里我以编程方式创建的行。
这里是code的简化样本。
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));
}
当我最初运行我的应用程序,为当前日期场均正确列出。然后,一旦我点击下周链接,它应该repload这个页面,并显示该新的一周的比赛。相反,我得到一个错误消息指定的孩子已经有了父母,你必须在孩子的父母先调用removeView()。我添加了一个tlGames.removeAllViews();该TableLayout tlGames线后立即但这并没有纠正错误。
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.
唯一的地方是我有addView是表中的行创建表行或textviews。
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?
感谢
推荐答案
我的猜测是,与页面的重新加载到这个星期显示的游戏中,你使用布局充气膨胀的细节布局?
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.
即
inflater.inflate(R.layout.profile, container, false);
这篇关于&QUOT;指定的子已具有父&QUOT;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!