Android动态表格布局-添加视图抛出异常IllegalStateException(子级已经有父级)
在 Java 中创建所需的 TableLayout 数量的布局并不称为设计时。
当我调用 createPlayerTables()
时,我收到一个 IllegalStateException
告诉我在将 View
(从其当前父级)分配给另一个父级之前将其删除
当我尝试将 ImageView 从 ImageView 列表添加到第一个 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));
}
该错误表明 ImageView 已已添加到 ViewGroup,但是看到下面的代码,我创建了新的 ImageView,并且只将它们添加到错误所在行的 ViewGroup 中,所以我不确定为什么会失败。
// 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));
}
...
}
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));
}
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此循环中:
您只需不断向
tableRowsLst
添加新的TableRows
,但始终只使用前三个元素。在循环之前清除列表:
In this loop:
you just keep adding new
TableRows
totableRowsLst
, but you always only use the first three elements.Clear the list before the loop:
尽管本示例中并非如此,但导致此问题的另一个常见原因是未正确使用
onCreateDialog()
和onPrepareDialog()
。onCreateDialog()
仅被调用一次,此处所做的任何操作都将持续存在。如果您要将动态内容添加到布局(Dialog)
,您可能需要使用onPrepareDialog()
,它将在创建之后但每次显示之前发生。引用 Android 文档:Although not the case in this example, another common cause of this problem is not correctly utilizing
onCreateDialog()
andonPrepareDialog()
. TheonCreateDialog()
is called only once and anything done here will persist. If you are adding dynamic content to a layout(Dialog)
, you probably want to useonPrepareDialog()
which will happen after create but before each display. To quote from the Android documentation:啊哈!好吧,在几次错误的开始之后,问题就来了。
imageViewsList
是一个成员变量。每次调用 createPlayerTables 时都会添加 6 个视图,然后每次都使用前 6 个视图。第一遍(玩家 0),没问题。第二遍(玩家 1):繁荣。选项 1) 不保存它们。给定的代码不需要它们,尽管这并没有涵盖所有基础。你可以把它们从桌子上的行中挖出来,然后在紧要关头扔掉。
选项 2) 将您对 imageViewsList 的访问偏移
playerNum * 6
(当首次调用createPlayerTables()
时,这将== imageViewsList.size()
)友好的建议:您可以通过几种不同的方式发现问题:
每次发现错误时,问问自己“我可以做什么来捕获这个问题”,这将极大地提高您的调试技能。
Aha! Okay, after a couple false starts, here's the problem.
imageViewsList
is a member variable. You're adding 6 views every time you call createPlayerTables, THEN USING THE FIRST 6 each time. First pass (player 0), no problem. Second pass (player 1): boom.Option 1) Don't save them. The given code doesn't need them, though that doesn't cover all the bases by any stretch. You could dig them out of the table rows and cast them in a pinch.
Option 2) Offset your access to imageViewsList by
playerNum * 6
(which will== imageViewsList.size()
whencreatePlayerTables()
is first called)Friendly advice: You could have found the problem in a couple different ways:
Asking yourself "what could I have done to catch this" every time you've figured out a bug will improve your debugging skills IMMENSELY.