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

时间:2017-03-28 12:36:04

标签: android android-fragments android-support-library

我的代码如下所示,当popBackStackImmediate重新出现时,我的片段会fragmentEmpty。所有这些都适用于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) 

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

然而,它还没有得到解决。我想知道是否有这个问题的解决方法,等待修复,而不是留在25.1.0?

1 个答案:

答案 0 :(得分:0)

在对25.1.0之前的片段和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()
    }

这将解决崩溃问题。