嘿所有人我正在开发一个播放YouTube视频的应用程序,我为应该播放视频的活动扩展YoutubeBaseActivity但是在达到活动时我一直收到错误。以下是log cat错误。
04-30 09:11:18.367: E/AndroidRuntime(26832): FATAL EXCEPTION: main
04-30 09:11:18.367: E/AndroidRuntime(26832): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.dclmhq.streamingapp/com.dclmhq.disuplytbvid.PlaySelectedVideo}: java.lang.ClassCastException: com.dclmhq.disuplytbvid.PlaySelectedVideo cannot be cast to com.google.android.youtube.player.YouTubePlayer$OnInitializedListener
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.os.Looper.loop(Looper.java:153)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.main(ActivityThread.java:5297)
04-30 09:11:18.367: E/AndroidRuntime(26832): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:11:18.367: E/AndroidRuntime(26832): at java.lang.reflect.Method.invoke(Method.java:511)
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-30 09:11:18.367: E/AndroidRuntime(26832): at dalvik.system.NativeStart.main(Native Method)
04-30 09:11:18.367: E/AndroidRuntime(26832): Caused by: java.lang.ClassCastException: com.dclmhq.disuplytbvid.PlaySelectedVideo cannot be cast to com.google.android.youtube.player.YouTubePlayer$OnInitializedListener
04-30 09:11:18.367: E/AndroidRuntime(26832): at com.dclmhq.disuplytbvid.PlaySelectedVideo.onCreate(PlaySelectedVideo.java:33)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.Activity.performCreate(Activity.java:5122)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-30 09:11:18.367: E/AndroidRuntime(26832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
04-30 09:11:18.367: E/AndroidRuntime(26832): ... 11 more
这是我的代码
public class PlaySelectedVideo extends YouTubeBaseActivity {
MediaPlayer mPlayer;
Intent receInt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playyoutubevideo);
receInt = getIntent();
TextView titleView = (TextView) findViewById(R.id.playerTitle);
TextView descView = (TextView) findViewById(R.id.playerDesc);
titleView.setText(receInt.getStringExtra("com.xxx.disuplytbvid.TITLE"));
descView.setText(receInt.getStringExtra("com.xxx.disuplytbvid.DESCRIPTION"));
YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(API_KEY, (YouTubePlayer.OnInitializedListener)this);
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
if (!wasRestored) {
String id = receInt.getStringExtra("com.XXX.disuplytbvid.vID");
player.cueVideo(id);
}
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/playerTitle"
android:textAppearance="@android:style/TextAppearance.Large"
android:textStyle="bold"
android:gravity="center"
android:text="@string/playerview_title"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/playerDesc"
android:textAppearance="@android:style/TextAppearance.Medium"
android:gravity="center"
android:text="@string/playerview_desc"/>
</LinearLayout>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
答案 0 :(得分:0)
尝试使用 YouTubeFailureRecoveryActivity 而非 YouTubeBaseActivity 来扩展 PlaySelectedVideo 活动。
而不是这个
youTubeView.initialize("AIzaSyAYBzv-h4SrKw16L-IXczrkXkVeX21fyU8", (YouTubePlayer.OnInitializedListener)this);
试
youTubeView.initialize("AIzaSyAYBzv-h4SrKw16L-IXczrkXkVeX21fyU8", YouTubeBaseActivity.this);
答案 1 :(得分:0)
你不仅要扩展
YouTubeBaseActivity but also Implement YouTubePlayer.OnInitializedListener
并实施所需的方法。