4

新しいYouTube Player API for Androidを使用してレイアウトを実装しようとしています。現在、私は単純なレイアウトを持っています:

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

現在、私の活動では、次のことを行っています。

public class MainActivity extends FragmentActivity implements YouTubePlayer.OnInitializedListener

レイアウトでフラグメントを使用するということは、(android.support.v4.app.FragmentActivity からの) FragmentActivity を使用する必要があるという印象を受けました。ただし、これを実行すると、次の例外が発生します。

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

これは、FragmentActivity の代わりに Activity を拡張すると機能します。どうすればこれを修正できますか?

4

1 に答える 1

14

YouTubePlayerFragment は android.app.Fragment を継承し、android.support.v4.app.Fragment のどこかにキャストします。古い API でこのクラスを使用する場合は、YouTubePlayerSupportFragment を使用します。それ以外の場合は、インポートを修正してください。

于 2013-01-01T23:11:37.627 に答える