Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception when using YouTubePlayerFragment with FragmentActivity and ActionBarSherlock

I am trying to implement a layout using the new YouTube Player API for Android . Currently, I have a simple layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  <fragment
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:id="@+id/youtube_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
  <TextView
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:textAppearance="@android:style/TextAppearance.Small"
      android:gravity="center"
      android:text="Nothin"/>
</LinearLayout>

Now, in my activity, I have the following:

public class MainActivity extends FragmentActivity implements YouTubePlayer.OnInitializedListener

I was under the impression that using a fragment in my layout, meant that I needed to use a FragmentActivity (which is from android.support.v4.app.FragmentActivity). However, I am getting the following exception when I run this:

java.lang.ClassCastException: com.google.android.youtube.player.YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment

This works when I extend Activity instead of FragmentActivity. How can I fix this?

like image 215
karkum Avatar asked Dec 08 '22 18:12

karkum


1 Answers

YouTubePlayerFragment inherits from android.app.Fragment, and you cast it somewhere to android.support.v4.app.Fragment. If you want to use this class on older API-s then use YouTubePlayerSupportFragment, otherwise fix your imports.

like image 197
marcinj Avatar answered Dec 11 '22 08:12

marcinj