我正在尝试在 Activity 中启动YouTube播放器。但是,我有时会不断收到此异常:
Fatal Exception: java.lang.IllegalStateException YouTubeServiceEntity not initialized
这是我在 Activity 中尝试初始化youtube播放器的方法。 (这是在OnCreate()内部完成的)
try {
final YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize("KEY", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean Boolean) {
if(ad==null || ad.getVideo_urls() == null)
return;
if (!Boolean)
{
try {
if (ad.getVideo_urls() != null && ad.getVideo_urls().length() > 0) {
String url = ad.getVideo_urls().getString(0);
if (url.contains("youtube")) {
String id = ad.getVideo_urls().getString(0).split("embed/")[1];
youTubeView.setVisibility(View.VISIBLE);
MyYouTubePlayer = youTubePlayer;
MyYouTubePlayer.cueVideo(id);
}
} else {
youTubeView.setVisibility(View.GONE);
Log.i(Constants.getTag(), "Video not found");
//Making sure the MyYouTubePlayer is null and if not is being released
if(MyYouTubePlayer != null)
{
MyYouTubePlayer.release();
}
}
}
catch (JSONException e) {
youTubePlayer.release();
e.printStackTrace();
}
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
youTubeView.removeAllViews();
}
});
} catch(Exception e){
e.printStackTrace();
Log.e("Youtube", "error initializing youtube");
}
最奇怪的是,这个问题不是持久的。它只是不时发生,我不知道为什么。你能告诉我是什么原因造成的吗?
这也是logcat:
java.lang.IllegalStateException: YouTubeServiceEntity not initialized
at android.os.Parcel.readException(Parcel.java:1433)
at android.os.Parcel.readException(Parcel.java:1379)
at com.google.android.youtube.player.internal.l$a$a.a()
at com.google.android.youtube.player.internal.o.a()
at com.google.android.youtube.player.internal.ad.a()
at com.google.android.youtube.player.YouTubePlayerView.a()
at com.google.android.youtube.player.YouTubePlayerView$1.a()
at com.google.android.youtube.player.internal.r.g()
at com.google.android.youtube.player.internal.r$c.a()
at com.google.android.youtube.player.internal.r$b.a()
at com.google.android.youtube.player.internal.r$a.handleMessage()
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(NativeStart.java)
最佳答案
我现在也正在我的应用程序中遇到此错误。当用户退出包含 Activity 的Youtube播放器 fragment 而未正确初始化时,就会发生问题。它是相当随机的,并且不容易复制。
我只是使用try-catch来避免它,幸运的是,它尚未被复制。
try {
mYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
} catch(IllegalStateException w){}
再次,这可能不是确定的解决方案。
一种解决方案是检查
OnBackPressed()
是否已初始化You-tube播放器。isInitializationComplete = false
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
isInitializationComplete = true;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
isInitializationComplete = true;
}
@Override
public void onBackPressed() {
if(!isInitializationComplete) return;
}
尝试猴子以更频繁地繁殖它。
adb shell monkey -p com.packagename -v 500
这是link,用于发布此错误的跟踪器。