将youtube api集成到我的应用程序后,该应用程序在非棒棒糖设备中完全正常工作。但是当我在Note 3 (lollipop OS)
中运行应用程序时,不幸的是应用程序停止抛出以下错误。
我的Youtube视频活动:
public class YoutubeVideo extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {
private static final int RECOVERY_DIALOG_REQUEST = 1;
// YouTube player view
private YouTubePlayerView youTubeView;
MovieActivity ma = new MovieActivity();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i("LOG", "Inside Youtube Act1");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
try {
setContentView(R.layout.youtube_activity);
Log.i("LOG", "Inside Youtube Act2"); //this wont execute as an exception is thrown
} catch (Exception e) {
Log.i("LOG", "Inside Youtube Act3");//this is executed
e.printStackTrace();
Log.i("LOG", e.getMessage());
}
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
Log.i("LOG", "Inside Youtube Act4");
Toast.makeText(YoutubeVideo.this, "Loading Video...Please wait", Toast.LENGTH_LONG).show();
// Initializing video player with developer key
youTubeView.initialize(Config.DEVELOPER_KEY, this);
Log.i("LOG", "Inside Youtube Act5");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
Log.i("LOG", "Inside YOutube Act7");
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
Log.i("LOG", "Inside YOutube Act6");
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
player.loadVideo(Config.YOUTUBE_VIDEO_CODE);
player.setFullscreen(true);
// Hiding player controls
player.setPlayerStyle(PlayerStyle.DEFAULT);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
@Override
public void onBuffering(boolean arg0) {
}
@Override
public void onPaused() {
}
@Override
public void onPlaying() {
}
@Override
public void onSeekTo(int arg0) {
}
@Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
@Override
public void onAdStarted() {
}
@Override
public void onError(ErrorReason arg0) {
}
@Override
public void onLoaded(String arg0) {
}
@Override
public void onLoading() {
}
@Override
public void onVideoEnded() {
finish();
}
@Override
public void onVideoStarted() {
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
// Retry initialization if user performed a recovery action
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}
}
我按照教程http://www.sitepoint.com/using-the-youtube-api-to-embed-video-in-an-android-app/
我不知道为什么当我点击Note 3中有lollipop的播放按钮时出现错误。在所有其他非棒棒糖设备应用程序完全正常。
我的xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".YoutubeVideo"
>
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Android清单文件:
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:windowSoftInputMode="adjustPan|adjustNothing"
android:name=".YoutubeVideo"
android:label="YoutubeVideo"
>
<intent-filter>
<action android:name="com.andyapps.tnow.YOUTUBEVIDEO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
错误日志:
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ android.view.InflateException: Binary XML file line #8: Error inflating class com.google.android.youtube.player.YouTubePlayerView
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.createView(LayoutInflater.java:640)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
02-14 10:07:05.593 19231-19231/com.andyapps.tnow W/System.err﹕ at android.transition.Scene.enter(Scene.java:175)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.transitionTo(PhoneWindow.java:483)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:426)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.Activity.setContentView(Activity.java:2267)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at com.andyapps.thetrailersnow.YoutubeVideo.onCreate(YoutubeVideo.java:43)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.Activity.performCreate(Activity.java:6283)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.ActivityThread.access$900(ActivityThread.java:177)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5942)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ Caused by: java.lang.reflect.InvocationTargetException
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Native Method)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
02-14 10:07:05.598 19231-19231/com.andyapps.tnow W/System.err﹕ at android.view.LayoutInflater.createView(LayoutInflater.java:614)
02-14 10:07:05.603 19231-19231/com.andyapps.tnow W/System.err﹕ ... 23 more
02-14 10:07:05.603 19231-19231/com.andyapps.tnow W/System.err﹕ Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
02-14 10:07:05.603 19231-19231/com.andyapps.tnow W/System.err﹕ at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
02-14 10:43:52.478 24585-24585/com.andyapps.tnow W/System.err﹕ at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
02-14 10:43:52.478 24585-24585/com.andyapps.tnow W/System.err﹕ ... 26 more