美文网首页
Fragment动画导致切换Fragment异常

Fragment动画导致切换Fragment异常

作者: 星空下奔跑 | 来源:发表于2018-11-16 17:53 被阅读0次

今天项目中发生了如下异常:

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

可是检查项目代码:

//        if (container != null && container.getChildCount() > 0) {
//           container.removeAllViews();
//        }
        if (contentView != null) {
            ViewGroup parent = (ViewGroup) contentView.getParent();

            if (parent != null) {
                parent.removeView(contentView);

            } else {
                LogUtil.v("BaseFragment", "error");
            }
            // unbinder = ButterKnife.bind(this, contentView);
        } else {
            this.layoutInflater = inflater;
            contentView = inflater.inflate(getContentLayoutID(), container, false);
            unbinder = ButterKnife.bind(this, contentView);
            initRecyclerView();
            setPresenter();
            // presenter = getPresenter();
            initView();
        }
        return contentView;

没有问题。却在快速切换时就会导致此异常的发生。之前从来没有过。
经过百度这篇文章发现了问题所在。
https://www.jianshu.com/p/2146ace8a244

原来以下移除ViewParent的操作:

public void endViewTransition(View view) {
 ··· 
if (view.mAttachInfo != null) {
 view.dispatchDetachedFromWindow(); 
}
 if (view.mParent != null) {
 view.mParent = null;
 } ·
·· invalidate();
 ··· }

只有在动画结束之后才会执行,如果在动画没有结束之前再次将view add到其他的viewgroup中就会引发此异常~

相关文章

网友评论

      本文标题:Fragment动画导致切换Fragment异常

      本文链接:https://www.haomeiwen.com/subject/zvzmfqtx.html