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

问题说明:

    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() 我们发现, 最终会调用到这个位置:

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

因为被移除的视图在mTransitioningViews中,(正在执行移除动画)因此没有将他的parent 设置为空。 然后导致添加view 的时候,崩溃了。

通过分析,找到了导致问题的原因。

public void startViewTransition(View view) {
        if (view.mParent == this) {
            if (mTransitioningViews == null) {
                mTransitioningViews = new ArrayList<View>();
            }
            mTransitioningViews.add(view);
        }
    }

 

转载于:https://my.oschina.net/u/592116/blog/3021610

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值