码迷,mamicode.com
首页 > 其他好文 > 详细

Call removeView() on the child's parent first

时间:2015-04-10 21:52:19      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

extends:http://stackoverflow.com/questions/6526874/call-removeview-on-the-childs-parent-first

 

Exception:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child‘s parent first.
           at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
           at android.view.ViewGroup.addView(ViewGroup.java:1871)
           at android.view.ViewGroup.addView(ViewGroup.java:1828)
           at android.view.ViewGroup.addView(ViewGroup.java:1808)
Solution:

((ViewGroup)scrollChildLayout.getParent()).removeView(scrollChildLayout); 

Use the child element to get a reference to the parent. Cast the parent to a ViewGroup so that you get access to the removeView method and use that.
     LinearLayout layout = initLayout();
        for (int i = 0; i < items.size(); i++) { 
            View view = items.get(i);
            ((ViewGroup) view.getParent()).removeView(view);
            layout.addView(view, lpLabel);
        }
        mLayout.addView(layout);

 

Call removeView() on the child's parent first

标签:

原文地址:http://www.cnblogs.com/niray/p/4415516.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!