SDK 25.1.1 NPE:尝试在空对象引用上调用虚拟方法 'int android.view.View.getVisibility()'

SDK 25.1.1 NPE: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference

我的代码如下,当 fragmentEmpty 重新出现时,我的片段将 popBackStackImmediate。在 25.1.0 及之前版本的 Support SDK 中一切正常。

class FragmentEmpty : Fragment() {

    var fragmentTwo : Fragment? = null
    var fragmentOne : Fragment? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_empty, container, false)
    }


    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (childFragmentManager.findFragmentByTag("FragmentOne") == null) {
            fragmentOne = FragmentOne()
            childFragmentManager.beginTransaction().add(R.id.container, fragmentOne, "FragmentOne").commit()
        } else {
            fragmentOne = childFragmentManager.findFragmentByTag("FragmentOne")
        }

        if (childFragmentManager.findFragmentByTag("FragmentTwo") != null) {
            fragmentTwo = childFragmentManager.findFragmentByTag("FragmentTwo")
            childFragmentManager.popBackStackImmediate()
        } else {
            fragmentTwo = FragmentTwo()
        }

        val transitionInflater = TransitionInflater.from(activity)
        val sharedElement = transitionInflater.inflateTransition(R.transition.shared_transition)
        fragmentTwo?.sharedElementReturnTransition = sharedElement
        fragmentOne?.sharedElementEnterTransition = sharedElement
    }

    fun openFragmentTwo() {
        var fragmentTransaction = childFragmentManager.beginTransaction()
        fragmentTransaction = fragmentTransaction.addSharedElement(activity.findViewById(R.id.fragment_button), "my_transition")
        fragmentTransaction.replace(R.id.container, fragmentTwo, "FragmentTwo").addToBackStack("").commit()
    }

}

移动到 25.1.1 及更高版本时,例如25.2.0。甚至 25.3.0,我现在在 popBackStackImmediate

时遇到此错误
 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference
     at android.support.v4.app.FragmentTransitionCompat21.findNamedViews(FragmentTransitionCompat21.java:387)
     at android.support.v4.app.FragmentTransition.captureOutSharedElements(FragmentTransition.java:693)
     at android.support.v4.app.FragmentTransition.configureSharedElementsUnoptimized(FragmentTransition.java:605)
     at android.support.v4.app.FragmentTransition.configureTransitionsUnoptimized(FragmentTransition.java:300)
     at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:113)
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823)
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776)
     at myproject.com.myapplication.FragmentEmpty.onViewCreated(FragmentEmpty.kt:32)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1314)
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
     at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:807)
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2360)
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
     at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:823)
     at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:776)
     at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178) 

有这个 link 说它会被修复 https://code.google.com/p/android/issues/detail?id=234324&sort=-id&colspec=ID%20Type%20Status%20Owner%20Summary%20Sta

但是还没有修复。我想知道除了留在 25.1.0 之外,在等待修复期间是否有针对此问题的解决方法?

在对 25.1.0 之前的片段和 post 25.1.0 的片段进行一些研究后,导致流程不同的主要区别是将默认的 AllowOptimization 设置为从 true 变为false.

要在使用更高版本的 SDK 时恢复 25.1.0 的行为,可以在 fragmentTransaction 上设置 setAllowOptimization(true)

    fun openFragmentTwo() {
        var fragmentTransaction = childFragmentManager.beginTransaction()
        fragmentTransaction = fragmentTransaction.setAllowOptimization(true).addSharedElement(activity.findViewById(R.id.fragment_button), "my_transition")
        fragmentTransaction.replace(R.id.container, fragmentTwo, "FragmentTwo").addToBackStack("").commit()
    }

这将解决崩溃问题。