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:4417) at android.view.ViewGroup.addView(ViewGroup.java:4258) at android.view.ViewGroup.addView(ViewGroup.java:4198) at android.view.ViewGroup.addView(ViewGroup.java:4171) at androidx.fragment.app.g.a(SourceFile:216) at androidx.fragment.app.g.a(SourceFile:436) at androidx.fragment.app.g.b(SourceFile:60) at androidx.fragment.app.g.c(SourceFile:58) at androidx.fragment.app.g.a(SourceFile:22) at androidx.fragment.app.g.h(SourceFile:2) at androidx.fragment.app.g.w(SourceFile:3) at androidx.fragment.app.g$a.a(SourceFile:1) at androidx.activity.OnBackPressedDispatcher.a(SourceFile:12) at androidx.activity.ComponentActivity.onBackPressed(SourceFile:1) 复制代码
问题发现java
在开启了转场动画的Fragment中,从一个Fragment跳转到下一个Fragment中,在上一个转场动画未结束时,立刻返回上一个页面。先来分析下问题代码:android
private var mView: View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
if (mView == null) {
mView = inflater.inflate(rootLayoutId, container, false)
}
return mView
}
复制代码
问题就在于返回的mView
若是已存在,则可能已经有了parent,致使被二次添加,修改代码以下:数组
private var mView: View? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
if (mView == null) {
mView = inflater.inflate(rootLayoutId, container, false)
} else {
(mView?.parent as ViewGroup?)?.removeView(mView)
}
return mView
}
复制代码
可是悲催的是,问题依旧。bash
上面明明已经从parent中移除了fragment的mView啊,难道是移除不成功?咱们加一下打印:app
var parent = (rootView?.parent as ViewGroup?)
Log.d("onCreateView ", "$parent ")
if (parent != null) {
parent.removeView(rootView)
}
parent= (rootView?.parent as ViewGroup?)
Log.d("onCreateView ", "$parent ")
复制代码
果真,第二次打印的结果,parent仍然和第一次同样,不为null。问题定位到了,竟然是removeView
失败的问题。 咱们来分析下,找到抛出异常的代码源码:ide
private void addViewInner(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout) {
if (child.getParent() != null) {
throw new IllegalStateException("The specified child already has a parent. You must call removeView() on the child's parent first.");
} else {
...
}
复制代码
因此接下来须要找到将child.getParent()置空的代码,分析下为何没执行 removeView(View view)的过程会调用removeViewInternal
方法:布局
private void removeViewInternal(int index, View view) {
...省略若干代码.....
//判断当前的view 正在播放,或预约播放的动画
if (view.getAnimation() != null ||
(mTransitioningViews != null && mTransitioningViews.contains(view))) {
addDisappearingView(view);
} else if (view.mAttachInfo != null) {
view.dispatchDetachedFromWindow();
}
...省略若干代码.....
removeFromArray(start, count);
}
复制代码
// 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; //这段代码不知足调节没有执行
}
...省略若干代码.....
}
复制代码
因此发生这次崩溃的罪魁祸首就是mTransitioningViews
,看下它的定义:动画
// The set of views that are currently being transitioned. This list is used to track views
// being removed that should not actually be removed from the parent yet because they are
// being animated.
private ArrayList<View> mTransitioningViews;
复制代码
意思就是它是来存储有过渡动画的view的一个数组列表。由于它们已经设置了动画,所以实际上不该该从父视图中删除。 这里的过渡动画指的是布局容器动画(LayoutTransition),就是在添加、隐藏子view 的时候,会有动画效果。 看来问题并非Fragment的转场动画问题,而是设置了layoutAnimation,来看下布局文件,确实设置了android:animateLayoutChanges
ui
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" android:orientation="vertical">
复制代码
设置了该属性会调用:google
public void setLayoutTransition(LayoutTransition transition) {
if (mTransition != null) {
LayoutTransition previousTransition = mTransition;
previousTransition.cancel();
previousTransition.removeTransitionListener(mLayoutTransitionListener);
}
mTransition = transition;
if (mTransition != null) {
mTransition.addTransitionListener(mLayoutTransitionListener);
}
}
复制代码
而后再mLayoutTransitionListener会在开始动画时将view加入到mTransitioningViews中:
private LayoutTransition.TransitionListener mLayoutTransitionListener =
new LayoutTransition.TransitionListener() {
@Override
public void startTransition(LayoutTransition transition, ViewGroup container, View view, int transitionType) {
// We only care about disappearing items, since we need special logic to keep
// those items visible after they've been 'removed'
if (transitionType == LayoutTransition.DISAPPEARING) {
startViewTransition(view);
}
}
...省略若干代码.....
};
复制代码
能够取消设置android:animateLayoutChanges
属性,也能够先清除下动画,再移除View:
parent.endViewTransition(mView)
mView!!.clearAnimation()
parent.removeView(mView)
复制代码