Android的动态表格布局 - 添加视图抛出异常IllegalStateException异常(孩子已经有父) [英] Android dynamic table layout - adding views throws exception IllegalStateException (child already has a parent)
问题描述
创建Java中的布局的要求是不知道作为designtime TableLayouts的数目
我得到一个 IllegalStateException异常
告诉我删除其分配给另一个查看
(从它的当前父)前父母,当我打电话 createPlayerTables()
该异常被抛出在这个循环中的第一行,当我尝试从ImageViews的列表中添加的ImageView到第一的TableRow:
的for(int i = 0;我3;;我++){
tableRowsLst.get(0).addView((ImageView的)imageViewsLst.get(ⅰ));
tableRowsLst.get(1).addView((ImageView的)imageViewsLst.get第(i + 3));
}
该错误表明ImageView的有已经被添加到一个ViewGroup中,但看到code以下,我创造新的ImageViews,我只把它们添加到一个ViewGroup中的行它的错误时,所以我不知道为什么它的失败。
//名单,其中,ImageView的> imageViewsLst =新...
//名单,其中,的TableRow> tableRowsLst =新...
/ **
*初始化的TableLayouts,每球员
* /
私人TableLayout createPlayerTables(INT playerNum){
...
的for(int i = 0; I< 6,我++){
imageViewsLst.add(新ImageView的(这个));
...
}
的for(int i = 0;我3;;我++){
tableRowsLst.add(新的TableRow(本));
...
}
的for(int i = 0;我3;;我++){
tableRowsLst.get(0).addView((ImageView的)imageViewsLst.get(ⅰ));
tableRowsLst.get(1).addView((ImageView的)imageViewsLst.get第(i + 3));
}
...
}
在这个循环:
的for(int i = 0;我3;;我++){
tableRowsLst.add(新的TableRow(本));
tableRowsLst.get(我).setLayoutParams(
新TableLayout.LayoutParams(LayoutParams.FILL_PARENT,dipToPixels(55)));
tableRowsLst.get(ⅰ).setOrientation(LinearLayout.HORIZONTAL);
}
你只是不断添加新的 TableRows
到 tableRowsLst
,但你总是只用前三个元素。
清除列表循环之前:
tableRowsLst.clear();
Creating a layout in Java as the number of TableLayouts required is not known as designtime.
I get an IllegalStateException
telling me to remove the View
(from it's current parent) before assigning it to another parent, when I call createPlayerTables()
The exception is thrown at the first line in this loop, when I try to add an ImageView from the List of ImageViews to the first TableRow:
for (int i = 0; i < 3; i++) {
tableRowsLst.get(0).addView((ImageView) imageViewsLst.get(i));
tableRowsLst.get(1).addView((ImageView) imageViewsLst.get(i+3));
}
The error suggests that the ImageView has already been added to a ViewGroup, but seeing the code below, I create new ImageViews, and I only add them to an ViewGroup at the line that it errors at, so I'm not sure why it's failing.
// List<ImageView> imageViewsLst = new ...
// List<TableRow> tableRowsLst = new ...
/**
* Initialises the TableLayouts, one per player
*/
private TableLayout createPlayerTables(int playerNum) {
...
for (int i = 0; i < 6; i++) {
imageViewsLst.add(new ImageView(this));
...
}
for (int i = 0; i < 3; i++) {
tableRowsLst.add(new TableRow(this));
...
}
for (int i = 0; i < 3; i++) {
tableRowsLst.get(0).addView((ImageView) imageViewsLst.get(i));
tableRowsLst.get(1).addView((ImageView) imageViewsLst.get(i+3));
}
...
}
In this loop:
for (int i = 0; i < 3; i++){
tableRowsLst.add(new TableRow(this));
tableRowsLst.get(i).setLayoutParams(
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, dipToPixels(55)));
tableRowsLst.get(i).setOrientation(LinearLayout.HORIZONTAL);
}
you just keep adding new TableRows
to tableRowsLst
, but you always only use the first three elements.
Clear the list before the loop:
tableRowsLst.clear();
这篇关于Android的动态表格布局 - 添加视图抛出异常IllegalStateException异常(孩子已经有父)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!