Android,取代YouTubePlayerFragment onInitializationFailure()

时间:2016-05-27 20:22:26

标签: java android android-fragments android-lifecycle android-youtube-api

在我的Android应用程序中,我通过YouTubePlayerFragmentFragment放在另一个FrameLayout中,我只需将Youtubeplayer添加到其中YouTubePlayerFragment。 这一切都按预期工作。 现在,如果手机没有安装Youtube App,这是Youtube API运行所必需的,我不想通过FragmentTransactionboolean youtubeSuccess添加到根片段。

我想到的只是创建一个onInitializationFailure并将其设置为true,如果.... if (youtubeSuccess) { FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit(); } return inflater.inflate(layoutResId, container, false); ..... 被调用,我将其设置为false。所以稍后我可以简单地使用它:

public class SampleYouTubeSlide extends Fragment {

    private FragmentActivity myContext;
    private static final String ARG_LAYOUT_RES_ID = "layoutResId";
    private int layoutResId;
    private YouTubePlayer YPlayer;
    private static final String YoutubeDeveloperKey = "XXXXXXXXXXXXXXXXX";
    private static final int RECOVERY_DIALOG_REQUEST = 1;
    static boolean youtubeSuccess;


    public SampleYouTubeSlide() {
    }


    @Override
    public void onAttach(Activity activity) {

        if (activity instanceof FragmentActivity) {
            myContext = (FragmentActivity) activity;
        }

        super.onAttach(activity);
    }


    public static SampleYouTubeSlide newInstance(int layoutResId) {
        SampleYouTubeSlide sampleSlide = new SampleYouTubeSlide();

        Bundle args = new Bundle();
        args.putInt(ARG_LAYOUT_RES_ID, layoutResId);
        sampleSlide.setArguments(args);

        return sampleSlide;
    }

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

        if (getArguments() != null && getArguments().containsKey(ARG_LAYOUT_RES_ID))
            layoutResId = getArguments().getInt(ARG_LAYOUT_RES_ID);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();

        youTubePlayerFragment.initialize(YoutubeDeveloperKey, new YouTubePlayer.OnInitializedListener() {

            @Override
            public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {

               youtubeSuccess = true;

                if (!wasRestored) {
                    YPlayer = player;
                    YPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
                    //YPlayer.setFullscreen(true);
                    YPlayer.cueVideo(getResources().getString(R.string.instructionsVideoId), 1);
                    //YPlayer.loadVideo(getResources().getString(R.string.instructionsVideoId));
                    //YPlayer.play();

                }


            }

            @Override
            public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
                // Called when initialization of the player fails.
                youtubeSuccess = false;

            }


        });

        if (youtubeSuccess) {
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
        }

        return inflater.inflate(layoutResId, container, false);
    }

}

但不知怎的,我的代码无效,我仍然显示错误消息:enter image description here

现在我不知道我是否正确理解生命周期,或者我是否盲目找到明显的错误。如果,为什么youtubeSuccess永远不会被设置?

完整代码:

count = dataTable.DefaultView.ToTable(true, "employeeid").rows.count

0 个答案:

没有答案