gpt4 book ai didi

android - 如何在我的 SupportFragment 中添加 YouTube 播放器?

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:50 26 4
gpt4 key购买 nike

我的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Container.MainBackground"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:layout_alignParentTop="true"
android:id="@+id/youtube_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

fragment 中的代码

public class VideoFragment extends YouTubePlayerSupportFragment implements YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "MyKey";
static private final String VIDEO = "ToMpzhdUD1Q";
static private final String VIDEO1 = "K77avo920Jc";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View videoView = inflater.inflate(R.layout.video_fragment, container, false);
getActivity().setTitle("Youtube");

YouTubePlayerSupportFragment youTubePlayerSupportFragment = new YouTubePlayerSupportFragment();
youTubePlayerSupportFragment.initialize(DEVELOPER_KEY, this);

return videoView;
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

List<String> list = new ArrayList<>();
list.add(VIDEO);
list.add(VIDEO1);
youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
youTubePlayer.cueVideos(list);

}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

Toast.makeText(getContext(), "FAIL!" + youTubeInitializationResult.toString(), Toast.LENGTH_LONG)
.show();

}
}

在主要 Activity 中:

getSupportFragmentManager().beginTransaction().replace(R.id.main_container,fragment).addToBackStack(null).commit();

当我尝试在抽屉中打开我的 Fragment 时出错:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.youtube.player.YouTubePlayerView.a()' on a null object reference at com.google.android.youtube.player.YouTubePlayerSupportFragment.onStart(Unknown Source)

最佳答案

这里的问题是您覆盖了父类(super class) YouTubePlayerSupportFragment 中的 onCreateView() 方法,该方法不是抽象的,实际上有一个实现,如看到:

    public View onCreateView(LayoutInflater var1, ViewGroup var2, Bundle var3) {
this.c = new YouTubePlayerView(this.getActivity(), (AttributeSet)null, 0, this.a);
this.a();
return this.c;
}

实际的变量名和类型超出了这个答案的范围。这里重要的是 YouTubePlayerView 在这里被实例化,你覆盖了这个方法,所以 YouTubePlayerViewonStart() 中被调用时是 null方法(也可在 YouTubePlayerSupportFragment 中找到)。

    public void onStart() {
super.onStart();
this.c.a();
}

因此,基本上,您唯一需要做的就是实例化您的 VideoFragment 类,如果您想要实际的视频而不是黑框,则需要初始化 fragment ,位于它的创建时间,(像这样:)

    public VideoFragment()
{
this.initialize("yourAPIKeyHere", this);
}

关于android - 如何在我的 SupportFragment 中添加 YouTube 播放器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38128042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com