- 异常
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:4465)
at android.view.ViewGroup.addView(ViewGroup.java:4301)
at android.view.ViewGroup.addView(ViewGroup.java:4242)
at android.view.ViewGroup.addView(ViewGroup.java:4215)
- 异常的原因
child.getParent()!=null
//ViewGroup中
private void addViewInner(View child, int index, LayoutParams params,
boolean preventRequestLayout) {
if (mTransition != null) {
// Don't prevent other add transitions from completing, but cancel remove
// transitions to let them complete the process before we add to the container
mTransition.cancel(LayoutTransition.DISAPPEARING);
}
if (child.getParent() != null) {
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
}
//省略代码。。。。
}
-
view.getParent
初始化及重置-
初始化
//无法通过View重置child.mParent,只能使用ViewGroup中的removeXXX方法进行重置,下面是View.mParent初始化方法,位于View中 /* * Caller is responsible for calling requestLayout if necessary. * (This allows addViewInLayout to not request a new layout.) */ void assignParent(ViewParent parent) { if (mParent == null) { mParent = parent; } else if (parent == null) { mParent = null; } else { throw new RuntimeException("view " + this + " being added, but" + " it already has a parent"); } }
-
重置
//位于ViewGroup中,具体调用逻辑不再展开 // This method also sets the child's mParent to null private void removeFromArray(int index) { final View[] children = mChildren; if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { children[index].mParent = null; } final int count = mChildrenCount; if (index == count - 1) { children[--mChildrenCount] = null; } else if (index >= 0 && index < count) { System.arraycopy(children, index + 1, children, index, count - index - 1); children[--mChildrenCount] = null; } else { throw new IndexOutOfBoundsException(); } if (mLastTouchDownIndex == index) { mLastTouchDownTime = 0; mLastTouchDownIndex = -1; } else if (mLastTouchDownIndex > index) { mLastTouchDownIndex--; } } // This method also sets the children's mParent to null private void removeFromArray(int start, int count) { final View[] children = mChildren; final int childrenCount = mChildrenCount; start = Math.max(0, start); final int end = Math.min(childrenCount, start + count); if (start == end) { return; } if (end == childrenCount) { for (int i = start; i < end; i++) { children[i].mParent = null; children[i] = null; } } else { for (int i = start; i < end; i++) { children[i].mParent = null; } // Since we're looping above, we might as well do the copy, but is arraycopy() // faster than the extra 2 bounds checks we would do in the loop? System.arraycopy(children, end, children, start, childrenCount - end); for (int i = childrenCount - (end - start); i < childrenCount; i++) { children[i] = null; } } mChildrenCount -= (end - start); }
-
-
解决方法
View childView=...; ViewGroup parent=....; /** * 抛出异常的错误代码 * parent.removeView(child); * parent.addView(child); **/ parent.removeView(childView); //parent和childView.getParent可能不是同一对象 if(childView.getParent instanceof ViewGroup){ ((ViewGroup)child.getParent).removeView(childView); } parent.addView(childView);