java.lang.IllegalStateException: The specified child already has a parent. You must call removeView

来源:互联网 发布:周芷若结局 知乎 编辑:程序博客网 时间:2024/05/16 10:21

使用viewpager时遇到的一个问题:

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

错误的意思是孩子已经有父母 需要使用removeView() 方法先从父母上移除才能使用add()方法。

那么怎么解决呢?

获取孩子的父母级 使用removeView

解决办法是在instantiateItem中使用如下方式:

ViewGroup parent = (ViewGroup) v.getParent();
if (parent != null) {
parent.removeAllViews();
}
container.addView(v);

阅读全文
0 0
原创粉丝点击