具有自定义动画错误的SupportFragmentManager事务

时间:2018-09-29 08:41:30

标签: android kotlin android-9.0-pie

切换片段时,我在SupportFragmentManager事务上遇到了自定义动画的问题。我正在使用的代码已与不推荐使用的FragmentManager一起使用,但是鉴于Android P中已不推荐使用该代码,因此我正在使用支持版本。

val ft = supportFragmentManager.beginTransaction()
ft.setCustomAnimations(animationIn, animationOut)
ft.hide(mCurrentFragment!!)
ft.show(f)
ft.commit()
mCurrentFragment = f

指定隐藏自定义动画时,错误是NPE:

  

由以下原因引起:java.lang.NullPointerException
  在android.support.v4.app.FragmentManagerImpl.completeShowHideFragment(FragmentManager.java:1725)

我确实尝试将animationOut设置为0,以使其正常工作,但片段过渡效果不佳。

更新: 这些是动画项目:

animationIn

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together" >
    <objectAnimator
        android:interpolator="@android:interpolator/accelerate_cubic" 
        android:propertyName="x"
        android:duration="300"
        android:valueType="floatType"
        android:valueFrom="-500"
        android:valueTo="0"
        android:startOffset="300" />

    <objectAnimator
        android:propertyName="alpha"
        android:duration="300"
        android:valueType="floatType"
        android:valueFrom="0.3"
        android:valueTo="1"
        android:startOffset="300" />
</set>

animationOut

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together" >
    <objectAnimator
        android:propertyName="alpha"
        android:duration="300"
        android:valueType="floatType"
        android:valueFrom="1"
        android:valueTo="0"
        android:startOffset="0"/>
</set>

有人遇到过这个问题吗? 谢谢。

这是FragmentManagerImpl类中发生错误的部分:

void completeShowHideFragment(final Fragment fragment) {
    if(fragment.mView != null) {
        FragmentManagerImpl.AnimationOrAnimator anim = this.loadAnimation(fragment, fragment.getNextTransition(), !fragment.mHidden, fragment.getNextTransitionStyle());
        if(anim != null && anim.animator != null) {
            anim.animator.setTarget(fragment.mView);
            if(fragment.mHidden) {
                if(fragment.isHideReplaced()) {
                    fragment.setHideReplaced(false);
                } else {
                    final ViewGroup container = fragment.mContainer;
                    final View animatingView = fragment.mView;
                    // --- the NPE error occurs here
                    container.startViewTransition(animatingView);
                    anim.animator.addListener(new AnimatorListenerAdapter() {
                        public void onAnimationEnd(Animator animation) {
                            container.endViewTransition(animatingView);
                            animation.removeListener(this);
                            if(fragment.mView != null) {
                                fragment.mView.setVisibility(8);
                            }

                        }
                    });
                }
            } else {
                fragment.mView.setVisibility(0);
            }

            setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
            anim.animator.start();
        } else {
            if(anim != null) {
                setHWLayerAnimListenerIfAlpha(fragment.mView, anim);
                fragment.mView.startAnimation(anim.animation);
                anim.animation.start();
            }

            int visibility = fragment.mHidden && !fragment.isHideReplaced()?8:0;
            fragment.mView.setVisibility(visibility);
            if(fragment.isHideReplaced()) {
                fragment.setHideReplaced(false);
            }
        }
    }

    if(fragment.mAdded && fragment.mHasMenu && fragment.mMenuVisible) {
        this.mNeedMenuInvalidate = true;
    }

    fragment.mHiddenChanged = false;
    fragment.onHiddenChanged(fragment.mHidden);
}

0 个答案:

没有答案