【问题标题】:Cannot do FragmentTransaction with YoutubePlayerSupportFragment无法使用 YoutubePlayerSupportFragment 进行 FragmentTransaction
【发布时间】:2019-09-09 13:53:16
【问题描述】:

在我的应用程序中,我使用BottomNavigationViewEx 库在片段之间切换,在其中一个片段中,我使用 YoutubePlayerSupportFragment 来显示 youtube 播放器。但是,似乎不可能对事务进行分段,因为 YoutubePlayerSupportFragment 似乎不兼容。

类型不匹配。

必需:片段

找到:MidiaFragment

MainActivity 中的底部导航:

viewEx.onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        val fragmentManager = supportFragmentManager
        val fragmentTransaction = fragmentManager.beginTransaction()

        when (item.itemId) {
            R.id.bnv_home -> {
                fragmentTransaction.replace(R.id.viewPager, FeedFragmentBeta()).commit()
                return@OnNavigationItemSelectedListener true
            }
            R.id.ic_destaques -> {
                fragmentTransaction.replace(R.id.viewPager, DestaquesFragment()).commit()
                return@OnNavigationItemSelectedListener true
            }
            R.id.ic_camera -> {
                fragmentTransaction.replace(R.id.viewPager, ChatDeMidiaFragment()).commit()
                return@OnNavigationItemSelectedListener true
            }
            R.id.ic_chat -> {
                fragmentTransaction.replace(R.id.viewPager, ChatFragment()).commit()
                return@OnNavigationItemSelectedListener true
            }
            R.id.bnv_perfil -> {
                fragmentTransaction.replace(R.id.viewPager, PerfilFragment()).commit()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

片段:

class MidiaFragment : YouTubePlayerSupportFragment(), YouTubePlayer.OnInitializedListener {

private val GOOGLE_API_KEY = "ABC..."
lateinit var youtubePlayerView: YouTubePlayerView

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View {

    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_midia, container, false)

    youtubePlayerView = view.findViewById(R.id.youtubePlayerView)
    youtubePlayerView.initialize(GOOGLE_API_KEY,this)


    return view

}

override fun onInitializationSuccess(provider: YouTubePlayer.Provider, youTubePlayer: YouTubePlayer, wasRestored: Boolean) {

    // l2UDgpLz20M
    // 1-AozVirb88
    // PLOcMSsuppV4q38HhyXXRPxFpwbX_04jf3

    //youTubePlayer.cueVideo("l2UDgpLz20M")
    youTubePlayer.loadVideo("l2UDgpLz20M")

}

override fun onInitializationFailure(provider: YouTubePlayer.Provider, youTubeInitializationResult: YouTubeInitializationResult) {

}

布局:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorBackgroundNightBlue"
    tools:context=".Fragment.MidiaFragment">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubePlayerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"/>

</FrameLayout>

打开片段会使应用程序崩溃。 如何解决此错误?

【问题讨论】:

    标签: android kotlin android-youtube-api


    【解决方案1】:

    请看一下这个答案,已经给出了一个Java实现,也许它可以帮助: https://stackoverflow.com/a/26459181/909317

    【讨论】:

      猜你喜欢
      • 2016-01-10
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-16
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多