首页 > 解决方案 > 与 Youtube Player for Publishers 不兼容的片段类型

问题描述

我尝试在我们的 androidx 应用程序中添加最后一个 Youtube Player for Publishers v2.1.2。要添加这个播放器,我们必须创建一个新的YoutubeEmbedFragment extends Fragment.

这里的问题是这个片段扩展android.app.Fragment并且在我们的应用程序中我们使用androidx.appcompat.app.AppCompatActivityandroidx.fragment.app所以当我想添加这个 Youtube 播放器片段时:

getChildFragmentManager()
    .beginTransaction()
    .replace(R.id.player_container, youTubeEmbedFragment)
    .commit();

它不会编译,因为

第二个参数类型错误。找到:'com.google.android.youtube.player.YouTubeEmbedFragment',需要:'androidx.fragment.app.Fragment'

我们如何在使用支持片段的同时也使用库?

标签: androidandroid-fragmentsandroidxyoutubeplayer

解决方案


我们目前在我们的应用程序中使用 AndroidX,我们遇到了同样的问题:

在此处输入图像描述

面向发布者的 Youtube 播放器 v2.1.2。未使用 AndroidX,因此您必须使用“完全限定名称”来访问该类android.app.FragmentTransaction并使用replace()需要android.app.Fragmentfrom which youTubeEmbedFragmentextends 的方法:

在此处输入图像描述


推荐阅读