android - 将 YouTubePlayerFragment 与 FragmentActivity 和 ActionBarSherlock 一起使用时出现异常

标签 android android-fragments youtube-api android-actionbar

我正在尝试使用新的 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>

现在,在我的 Activity 中,我有以下内容:

public class MainActivity extends FragmentActivity implements YouTubePlayer.OnInitializedListener

我的印象是在布局中使用 fragment 意味着我需要使用 FragmentActivity(来自 android.support.v4.app.FragmentActivity)。但是,当我运行它时出现以下异常:

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

这在我扩展 Activity 而不是 FragmentActivity 时有效。我该如何解决这个问题?

最佳答案

YouTubePlayerFragment 继承自 android.app.Fragment,您将其转换为 android.support.v4.app.Fragment。如果您想在较旧的 API 上使用此类,请使用 YouTubePlayerSupportFragment,否则请修复您的导入。

关于android - 将 YouTubePlayerFragment 与 FragmentActivity 和 ActionBarSherlock 一起使用时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14115008/

相关文章:

android - RecognitionListener.onError() 是否自动 SpeechRecognizer.cancel()?

Android map ,在 Overlay 构造函数中绘制

android - 无法使用 Material 设计库

java - ImageView 应该位于所有内容之上,例如 float 操作按钮

video - Youtube api v3 无法从 channel 中检索所有视频

gwt - 如何从 GWT 应用程序进行 YouTube API 调用

java - 找出必填字段以填写pdf文件

android - 键盘隐藏时显示 View ,反之亦然

java - 无法在 Wowza GoCoder 的 PlayerActivity 中播放视频

Iphone Youtube 全屏 Web 应用程序(独立)无音频(BUG ?)