【问题标题】:When adding view inside layout and when removing and re-adding IllegalStateException在布局内添加视图以及删除和重新添加 IllegalStateException 时
【发布时间】:2014-04-05 20:50:15
【问题描述】:

在布局中添加视图以及删除和重新添加时,它会像这样抛出。

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);

但同样的问题!

【问题讨论】:

  • 以适当的方式编辑您的问题,提供更多信息以及您迄今为止尝试过的代码示例,以避免无限 -ve 投票。
  • linearLayout.addView(view);这条线经常被调用(每次活动恢复时),所以我添加了类似 ((ViewGroup)view.getParent()).removeAllViewsInLayout();线性布局.removeView(view); if(linearLayout.getChildCount()==0) linearLayout.addView(view);

标签: android layout view android-linearlayout android-relativelayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    ((ViewManager)view.getParent()).removeView(view);
    

    成功了!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2018-12-26
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多