In my application I am using the BottomNavigationViewEx library to switch between fragments and in one of them I am using YoutubePlayerSupportFragment to display a youtube player. However it seems that it is not possible to fragment transaction because YoutubePlayerSupportFragment does not seem to be compatible.
Type mismatch.
Required: Fragment
Found: MidiaFragment
BottomNavigation in 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
}
Fragment:
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) {
}
Layout:
<?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>
Opening the fragment crashes the application. How to fix this error?