/ / Exception lors de l'utilisation de YouTubePlayerFragment avec FragmentActivity et ActionBarSherlock - Android, Android-fragments, youtube-api, android-actionbar

Exception lors de l'utilisation de YouTubePlayerFragment avec FragmentActivity et ActionBarSherlock - android, fragments Android, youtube-api, android-actionbar

J'essaie d'implémenter une mise en page à l'aide du nouveau API de lecteur YouTube pour Android . Actuellement, j'ai une mise en page simple:

<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>

Maintenant, dans mon activité, j'ai les éléments suivants:

public class MainActivity extends FragmentActivity implements YouTubePlayer.OnInitializedListener

J'avais l'impression d'utiliser un fragmentdans ma mise en page, cela signifiait que je devais utiliser FragmentActivity (qui provient de android.support.v4.app.FragmentActivity). Cependant, je reçois l’exception suivante lorsque j’exécute ceci:

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

Cela fonctionne lorsque j'étends Activité au lieu de FragmentActivity. Comment puis-je réparer cela?

Réponses:

14 pour la réponse № 1

YouTubePlayerFragment hérite d'android.app.Fragment, et vous le jetez quelque part à android.support.v4.app.Fragment. Si vous souhaitez utiliser cette classe sur d'anciennes API, utilisez YouTubePlayerSupportFragment, sinon corrigez vos importations.