8

我想在我的 Android Lollipop 应用中实现共享元素转换。在我阅读了文档、SO 问题和一些帖子之后,我决定试一试,但现在我遇到了问题。

场景是我有两个片段容器(用于平板电脑),就像正常的列表/详细信息设计模式一样。

我想在列表项被触摸时在列表片段到详细片段之间进行共享元素转换。细节片段的输入是可以的,但是当我按下返回按钮时,应用程序在转换框架代码中崩溃并出现 NullPointerException。

共享元素转换是否支持该场景?

下面是开始细节片段的代码:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = DetailFragment.create((int)id);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    View title = view.findViewById(R.id.item_name);
    title.setTransitionName("title");
    listFragment.setSharedElementReturnTransition(
        TransitionInflater.from(this).inflateTransition(R.transition.change_bounds));
    listFragment.setExitTransition(
        TransitionInflater.from(this).inflateTransition(android.R.transition.explode));

    fragment.setSharedElementEnterTransition(
        TransitionInflater.from(this).inflateTransition(R.transition.change_bounds));
    fragment.setEnterTransition(
        TransitionInflater.from(this).inflateTransition(android.R.transition.explode));
    ft.addSharedElement(title, "title");
}
else {
    ft.setCustomAnimations(R.anim.slide_in_right, 0, 0, R.anim.slide_out_right);
}
ft.replace(R.id.detail_panel, fragment)
   .addToBackStack(null)
   .commit();

Logcat 在这里:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v4.app.Fragment.getAllowReturnTransitionOverlap()' on a null object reference
        at android.support.v4.app.BackStackRecord.configureTransitions(BackStackRecord.java:1201)
        at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1029)
        at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:883)
        at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1541)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:502)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:176)
        ...
4

0 回答 0