Android Remove from parent 以后,addView 仍然崩溃问题

问题说明:java

    View 在屡次调用view.getParent().removeView(view);  以后,调用ViewGroup.addView(view )  程序崩溃。动画

if (child.getParent() != null) {
            throw new IllegalStateException("The specified child already has a parent. " +
                    "You must call removeView() on the child's parent first.");
        }

 

跟踪ViewGroup.removeView() 咱们发现, 最终会调用到这个位置:this

private void removeFromArray(int index) {
        final View[] children = mChildren;
        if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) {
            children[index].mParent = null;
        }

由于被移除的视图在mTransitioningViews中,(正在执行移除动画)所以没有将他的parent 设置为空。 而后致使添加view 的时候,崩溃了。code

经过分析,找到了致使问题的缘由。ci

public void startViewTransition(View view) {
        if (view.mParent == this) {
            if (mTransitioningViews == null) {
                mTransitioningViews = new ArrayList<View>();
            }
            mTransitioningViews.add(view);
        }
    }
相关文章
相关标签/搜索