我正在使用片段转换来为Recycler视图中的一个视图添加动画效果,使用了25.0.1支持库,对于目标片段的推入和弹出,转换效果很好。
我使用的代码如下:
supportFragmentManager.beginTransaction()
.replaceAddToBackStack(R.id.content, fragment)
.addSharedElement(fragment, commonViews)
.commit()
现在我正在尝试更新支持库的25.1.0版本,但是弹出动画崩溃了,因为转换找不到原始视图,并且会崩溃并显示以下错误信息:
E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.support.v4.app.FragmentTransitionCompat21.setNameOverridesOptimized(FragmentTransitionCompat21.java:339)
at android.support.v4.app.FragmentTransition.configureTransitionsOptimized(FragmentTransition.java:232)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:111)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2143)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:822)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:775)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:178)
at App.ui.base.BaseActivity.onBackPressed(BaseActivity.kt:23)
...
但是如果我禁用了"优化",它就可以正常工作。
supportFragmentManager.beginTransaction()
.replaceAddToBackStack(R.id.content, fragment)
.addSharedElement(fragment, commonViews)
.setAllowOptimization(false)
.commit()
我做错了什么?
编辑: 我正在使用以下扩展来包装共享元素:
.replaceAddToBackStack
fun<T : Fragment> FragmentTransaction.replaceAddToBackStack(containerView: Int, fragment: T, tag: String? = null) : FragmentTransaction {
return replace(containerView, fragment, tag).addToBackStack(fragment.toString())
}
.addSharedElement
fun<T: Fragment, V: View> FragmentTransaction
.addSharedElement(destination: T?, sharedViews: List<V>?,
sharedTransform: TransitionSet? = null,
enterTransition: Transition? = null,
exitTransition: Transition? = null
) : FragmentTransaction {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return this
}
if (destination == null || sharedViews == null) {
return this
}
val fade = Fade()
fade.duration = 150
destination.enterTransition = enterTransition ?: fade
destination.allowEnterTransitionOverlap = false
destination.allowReturnTransitionOverlap = false
destination.exitTransition = exitTransition ?: fade
destination.sharedElementEnterTransition = sharedTransform ?: DetailsTransition()
destination.sharedElementReturnTransition = sharedTransform ?: DetailsTransition()
sharedViews.forEach {
this.addSharedElement(it, it.transitionName)
}
return this
}
inName.equals(inNames.get(j))
。基本上导致这个NPE的原因是,在你尝试添加的片段中有一个共享视图具有空的转换名称。 - andrei_zaitcev