java.lang.IllegalStateException:使用YouTubePlayerApi时,YouTubeServiceEntity未初始化错误 [英] java.lang.IllegalStateException: YouTubeServiceEntity not initialized error when using YouTubePlayerApi

查看:94
本文介绍了java.lang.IllegalStateException:使用YouTubePlayerApi时,YouTubeServiceEntity未初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用了 YouTubePlayerAPI YouTubePlayerSupportFragment ,但出现以下错误,但我无法确定是什么原因引起的.我一直在寻找信息,但没有发现任何有用的信息.

I'm using YouTubePlayerAPI and YouTubePlayerSupportFragment in my app and I'm getting the following error, but I couldn't find out what is causing it. I've been looking for information but I haven't found anything useful.

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(Unknown Source)
    at com.google.android.youtube.player.internal.o.a(Unknown Source)
    at com.google.android.youtube.player.internal.ad.a(Unknown Source)
    at com.google.android.youtube.player.YouTubePlayerView.a(Unknown Source)
    at com.google.android.youtube.player.YouTubePlayerView$1.a(Unknown Source)
    at com.google.android.youtube.player.internal.r.g(Unknown Source)
    at com.google.android.youtube.player.internal.r$c.a(Unknown Source)
    at com.google.android.youtube.player.internal.r$b.a(Unknown Source)
    at com.google.android.youtube.player.internal.r$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

在stackstrace中没有任何行号指向我的任何班级或活动.

In the stackstrace there isn't any line number pointing to any of my classes or activities.

有什么想法吗?

谢谢!

编辑

我的自定义 YoutubePlayerFragment 类: YouTubeVideoPlayerFragment.java

public class YouTubeVideoPlayerFragment extends YouTubePlayerSupportFragment {


private static final String ARG_URL = "url";


// ===========================================================
// Constructors
// ===========================================================

/**
 * Mandatory empty constructor for the fragment manager to instantiate the
 * fragment (e.g. upon screen orientation changes).
 */
public YouTubeVideoPlayerFragment() {
}

/**
 * Factory method to generate a new instance of the fragment given a video URL.
 *
 * @param url The video url this fragment represents
 * @return A new instance of this fragment with itemId extras
 */
public static YouTubeVideoPlayerFragment newInstance(String url) {
    final YouTubeVideoPlayerFragment mFragment = new YouTubeVideoPlayerFragment();

    // Set up extras
    final Bundle args = new Bundle();
    args.putString(ARG_URL, url);
    mFragment.setArguments(args);

    // Initialize YouTubePlayer
    mFragment.init();

    return mFragment;
}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

private void init(){
    initialize(Constants.API_KEY, new YouTubePlayer.OnInitializedListener() {
        @Override
        public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {
            if (!wasRestored) {
                youTubePlayer.cueVideo(getArguments().getString(ARG_URL));
                youTubePlayer.setShowFullscreenButton(false);
            }
    }
}

fragment.xml

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/black" >

    <!-- For YoutubeFragment -->
    <FrameLayout
        android:id="@+id/youtube_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

调用方法:

// Create a new instance of YouTubeVideoPlayerFragment providing video id
        // and place it in the corresponding FrameLayout
        final YouTubeVideoPlayerFragment youTubeVideoPlayerFragment = YouTubeVideoPlayerFragment.newInstance(VIDEO_ID);
        final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
        ft.replace(R.id.youtube_fragment, youTubeVideoPlayerFragment);
        ft.commit();

编辑

我已经找到了该错误的起因.这是场景:

I've found out the origin of that error. This is the scenario:

活动开始.在 onCreate()中,其实例化一个新的 YouTubeVideoPlayerFragment ,并在其内部初始化 YouTube 对象(内部启动 YouTubeServiceEntity ) newInstance()方法.然后,在加载视频时,将之前实例化的 YouTube 片段与 FragmentManager 附加到相应的 FrameLayout 上.

The activity starts. In onCreate() it instantiates a new YouTubeVideoPlayerFragment and initializes YouTube object (which starts the YouTubeServiceEntity internally) in its newInstance() method. Then the YouTube fragment that was instantiated before, is attached with FragmentManager to the corresponding FrameLayout while video is loading.

这是问题所在:如果用户在视频加载之前退出活动,则会引发异常.

Here is the issue: If user exits the activity before video had been loaded, the exception is thrown.

因此,如果用户要在这种情况下退出活动,该怎么办,怎么办?我真的不知道该怎么办!

So if user want to exit from the activity in that case, what should I do and how? I don't really know what to do!

推荐答案

再次,请勿使用片段构造函数或工厂方法来处理生命周期或上下文绑定的实体.简而言之,此类实体只能在调用 super.onCreate(...)后使用.

Once again, do NOT use fragment constructors or factory methods to work with lifecycle or context bound entities. Simply put, such entities can only be used after super.onCreate(...) has been called.

现在的问题是,何时调用 init 方法?

The question now is, when to call the init method?

以下是 YouTubePlayerFragment 文档说:

Here's what YouTubePlayerFragment documentation says:

只要调用其 onDestroyView()方法,便会释放与此片段关联的 YouTubePlayer .因此,每当与该片段相关联的活动重新创建时,即使通过设置在活动重新创建中保留了片段实例,您都必须重新调用 initialize(String,YouTubePlayer.OnInitializedListener).setRetainInstance(boolean).

The YouTubePlayer associated with this fragment will be released whenever its onDestroyView() method is called. You will therefore have to re-call initialize(String, YouTubePlayer.OnInitializedListener) whenever the activity associated with this fragment is recreated, even if the fragment instance is retained across activity re-creation by setting setRetainInstance(boolean).

您可能会想将 init()放在 onActivityCreated 中,但是为时已晚,因为已经调用了 onStart 并且已经执行了布局.

You may be tempted to put init() in onActivityCreated but that's too late, since onStart was already called and layout already performed.

onDestroyView 相对应的是 onViewCreated ,这是理想的选择.

Counterpart to onDestroyView is onViewCreated and that's the perfect candidate.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    init();
}

根据建议,在片段的构造函数中调用 setRetainInstance(true).重新创建活动时,将不会重新创建片段,只有其UI会经历生命周期事件.

As suggested call setRetainInstance(true) in the fragment's constructor. When the activity is recreated the fragment will not be recreated, only its UI will go through lifecycle events.

这篇关于java.lang.IllegalStateException:使用YouTubePlayerApi时,YouTubeServiceEntity未初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆