我正在尝试创建一个类似于播放音乐的共享元素过渡元素。当您单击卡片时,专辑封面会展开到新片段中的 ImageView。然后,当您单击返回时,它会动画回到卡片中。我的进入动画运行良好,但问题是返回或退出过渡。每当我回到上一个片段时,应用程序就会崩溃。这是我得到的错误:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.support.v4.app.Fragment.getAllowReturnTransitionOverlap()”
这是我执行动画/过渡的方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
fragment.setSharedElementReturnTransition(TransitionInflater.from(fragment.getActivity()).inflateTransition(R.transition.change_image_transform));
fragment.setReturnTransition(TransitionInflater.from(fragment.getActivity()).inflateTransition(android.R.transition.explode));
Fragment replaceFragment = new AlbumPageFragment();
replaceFragment.setSharedElementEnterTransition(TransitionInflater.from(fragment.getActivity()).inflateTransition(R.transition.change_image_transform));
replaceFragment.setEnterTransition(TransitionInflater.from(fragment.getActivity()).inflateTransition(android.R.transition.explode));
ImageView image = (ImageView) layout.findViewById(R.id.album_card_album_art);
FragmentTransaction ft = fragment.getFragmentManager().beginTransaction()
.replace(R.id.content_frame, replaceFragment)
.addToBackStack("transaction")
.addSharedElement(image, "MyTransition");
ft.commit();
}
change_image_transform.xml
<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeImageTransform />
</transitionSet>
content_frame 位于活动的布局视图中,因此两个片段都包含在其中。
我的问题是:如何正确使用带有片段的共享元素转换?谢谢!