使用YouTubePlayerSupportFragment崩溃[英] crash using YouTubePlayerSupportFragment

本文是小编为大家收集整理的关于使用YouTubePlayerSupportFragment崩溃的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一项从片段延伸的活动

public class Youtube_visor extends Fragment{ ...

和一种使用YouTubePlayerSupportFragment

创建新的YouTube播放器的方法

这是我的代码:

public void playVideo () {

        fragmentManager = getActivity().getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
        fragmentTransaction.add(R.id.fragmentz, player); // fragmentz es un linearlayout que hay en el xml
        fragmentTransaction.commit();

        player.initialize(Static_generals.youtubeKey,new OnInitializedListener() {

            public void onInitializationSuccess(Provider arg0,
                    YouTubePlayer arg1, boolean arg2) {
                if (!arg2) {
                    arg1.cueVideo("3OhGkg_XT3o");
                }
            }

            @Override
            public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
                // Error
            }
        });
    }

这在平板电脑上非常有效,我有一个带有两个片段的活动,留下了视频列表,右,创建YouTube Player的片段

我的问题是我不想创建一个新的YouTubePlayer,我想在XML中声明一个并始终使用它. 这是XML中声明的片段:

   <fragment
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:id="@+id/youtube_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

现在,在我的代码中,我替换了这一行:

YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();

放这个新行:

YouTubePlayerSupportFragment player = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);

但总是崩溃. 我搜索了有关它的信息,但是没有太多可用,或者找不到它:)

我在做什么错?

我想我把我的碎片称为错误,因为当我做一个新的片段时,正常工作

我感谢任何帮助.

谢谢和善意

推荐答案

看起来您已经将YouTubePlayerFragment放在XML中,但是您正在尝试将其施加到代码中的YouTubePlayerSupportFragment.尝试替换此行:

android:name="com.google.android.youtube.player.YouTubePlayerFragment"

与此:

android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"

本文地址:https://www.itbaoku.cn/post/2091072.html

问题描述

I have an activity that extends from a fragment

public class Youtube_visor extends Fragment{ ...

and a method that creates a new YouTube player using YouTubePlayerSupportFragment

This is my code:

public void playVideo () {

        fragmentManager = getActivity().getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();

        YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();
        fragmentTransaction.add(R.id.fragmentz, player); // fragmentz es un linearlayout que hay en el xml
        fragmentTransaction.commit();

        player.initialize(Static_generals.youtubeKey,new OnInitializedListener() {

            public void onInitializationSuccess(Provider arg0,
                    YouTubePlayer arg1, boolean arg2) {
                if (!arg2) {
                    arg1.cueVideo("3OhGkg_XT3o");
                }
            }

            @Override
            public void onInitializationFailure(Provider arg0,YouTubeInitializationResult arg1) {
                // Error
            }
        });
    }

This works perfectly on the tablet, I have an activity with two fragments, left a list of videos, and right, the fragment which create the YouTube player

My problem is I do not want to create a new YouTubePlayer, I want to declare one in the xml and always use that. This is the fragment declared in the xml:

   <fragment
      android:name="com.google.android.youtube.player.YouTubePlayerFragment"
      android:id="@+id/youtube_fragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

Now, in my code, I replace this line:

YouTubePlayerSupportFragment player = new YouTubePlayerSupportFragment();

and put this new line:

YouTubePlayerSupportFragment player = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);

but always crash. I searched for information about it but there is not much available, or have not found it :)

What am I doing wrong?

I guess I'm calling my fragment wrong, because when I do a new one working properly

I appreciate any help.

Thanks and kind regards

推荐答案

Looks like you've put a YouTubePlayerFragment in your XML, but you're trying to cast it to a YouTubePlayerSupportFragment in the code. Try replacing this line:

android:name="com.google.android.youtube.player.YouTubePlayerFragment"

With this:

android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"