java.lang.ClassCastException : android. support.transition.Fade 无法转换为 android.transition.Transition?

标签 java android android-fragments android-animation

尝试添加动画以替换 fragment 时引发该异常。 根据这个答案,stackoverflow ,过渡框架不能应用于 fragment ,但这篇文章表明您可以 How to use Material Transitions in Fragment Transactions我尝试过的代码如下

final JobFragment jobFragment = JobFragment.newInstance(job);
    final Fragment previousFragment = fragmentManager.findFragmentById(R.id.content);
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //exit transition
    Fade exitFade = new Fade();
    exitFade.setDuration(1000);
    previousFragment.setExitTransition(exitFade);
    //enter transition
    Explode enterExplode = new Explode();
    enterExplode.setDuration(1000);
    jobFragment.setEnterTransition(enterExplode);

    fragmentTransaction.replace(R.id.content, jobFragment).addToBackStack("jobDetails")
            .commit();

最佳答案

答案就在你的标题中 - 你的导入错误,一个是支持库,另一个不是。检查你是否有一致的导入。

示例:

import android.support.transition.Fade;
import android.support.transition.Transition;

或者:

import android.transition.Fade;
import android.transition.Transition;

为了兼容性,请使用support(在 24.2.0 中添加)版本。

关于java.lang.ClassCastException : android. support.transition.Fade 无法转换为 android.transition.Transition?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45399946/

相关文章:

java - 如何使用 Java 中的 HashMap 一次替换文件中的多个字符串

android - 如何通过 scanRecord 识别 Eddystone

android - 火力数据库 : models with Kotlin delegated properties

android - 抽屉导航 - 默认显示主页 fragment

java - 从 Fragment 启动 Activity 抛出 ClassNotFoundException

android - 你如何重绘 fragment ?

java - 有没有办法减少另一个类的静态变量的长度?

java - 在 ZooKeeper 中,我可以在不存在的 znode 上使用 async getData() 吗?

Java:将 ArrayList<Byte> 转换为 ByteBuffer

android - Ionic 3 暂停和恢复事件在 Android 中触发多次