view有了父元素就不能再添加父元素

 

在往一个控件添加一个view后,再去添加一个view就会抛出异常出来 如下:

java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.

 

1 // 如果view有父元素  全部移除  否则子元素有父元素再添加父元素会报错
2             ViewGroup parent = (ViewGroup) view.getParent();
3             if(parent != null){
4                 parent.removeAllViews();
5             }

 

posted @ 2013-02-21 22:23  My_苦行僧  阅读(298)  评论(0编辑  收藏  举报