Android 在布局内部添加视图以及删除和重新添加IllegalStateException时
在布局内部添加视图时,以及在删除和重新添加视图时,它会像这样抛出Android 在布局内部添加视图以及删除和重新添加IllegalStateException时,android,layout,view,android-linearlayout,android-relativelayout,Android,Layout,View,Android Linearlayout,Android Relativelayout,在布局内部添加视图时,以及在删除和重新添加视图时,它会像这样抛出 The specified child already has a parent. You must call removeView() on the child's parent first 有什么想法吗 linearLayout.addView(view); 经常调用此行(即每次活动恢复时), 因此,我添加了如下内容: ((ViewGroup)view.getParent()).removeAllViewsInLayout
The specified child already has a parent. You must call removeView() on the child's parent first
有什么想法吗
linearLayout.addView(view);
经常调用此行(即每次活动恢复时),
因此,我添加了如下内容:
((ViewGroup)view.getParent()).removeAllViewsInLayout();
linearLayout.removeView(view);
if(linearLayout.getChildCount()==0)
linearLayout.addView(view);
但同样的问题 如果您在“活动”中动态创建布局的子项,如果返回则转到另一个“活动”,则必须删除视图并使用新信息重新创建
((ViewManager)view.getParent()).removeView(view);
这成功了 以适当的方式编辑您的问题,提供更多的信息以及您迄今为止尝试的代码示例,以避免无限制的-ve投票。linearLayout.addView(view);这一行经常被调用(每次活动恢复时),所以我添加了类似((ViewGroup)view.getParent()).removeAllViewsInLayout();线性布局。移除视图(视图);如果(linearLayout.getChildCount()==0)linearLayout.addView(视图);