在布局内部添加视图以及删除和重新添加IllegalStateException时

时间:2014-03-04 10:53:36

标签: android layout view android-linearlayout 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();
linearLayout.removeView(view);
if(linearLayout.getChildCount()==0)
    linearLayout.addView(view);

但同样的问题!

2 个答案:

答案 0 :(得分:0)

如果您在活动中动态创建布局的子项,并且如果返回则转到另一个活动,则必须删除视图并使用新信息重新创建它。

答案 1 :(得分:0)

((ViewManager)view.getParent()).removeView(view);

这工作!!!