gpt4 book ai didi

java - YouTube 播放器在选项卡式 Activity 中导致此错误

转载 作者:行者123 更新时间:2023-12-02 02:56:49 26 4
gpt4 key购买 nike

我有选项卡式 Activity ,我想在第三个选项卡上显示 YouTube 视频。

这是我的java:

public class FragmentTutorial extends YouTubePlayerFragment {

YouTubePlayerView youTubePlayerView;
YouTubePlayer.OnInitializedListener onInitializedListener;

public FragmentTutorial() {
// Required empty public constructor
}

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

youTubePlayerView = (YouTubePlayerView) youTubePlayerView.findViewById(R.id.tutorial_youtube_player);

onInitializedListener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("3LiubyYpEUk");
}

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

}
};

youTubePlayerView.initialize(PlayerConfig.API_KEY, onInitializedListener);

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tutorial, container, false);
}

}

这是我的 XML:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.FragmentTutorial">

<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tutorial_youtube_player" />

</FrameLayout>

这是我的 MainActivity :

class FragmentAdapter extends FragmentPagerAdapter {

public FragmentAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentHome();
case 1:
return new FragmentCatalog();
case 2:
return new FragmentTutorial();
}
return null;
}

@Override
public int getCount() {
return 3;
}


@Override
public CharSequence getPageTitle(int position) {
switch (position){
//
//Your tab titles
//
case 0: return "Home";
case 1: return "Catalog";
case 2: return "Tutorial";
default: return null;
}
}
}

当我尝试运行此代码时,出现以下错误:

Error:(80, 28) error: incompatible types: FragmentTutorial cannot be converted to Fragment

我在这里错过了什么?谢谢...

最佳答案

可能是由错误的 Fragment 类引起的,YouTubePlayerFragment 扩展了 android.app.FragmentFragmentPagerAdapter 需要 android.support .v4.app.Fragment 并且无法转换

编辑:

尝试使用android.support.v13.app.FragmentPagerAdapter代替android.support.v4.app.FragmentPagerAdapter(其他 fragment 应该扩展android.app . fragment )

关于java - YouTube 播放器在选项卡式 Activity 中导致此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42955010/

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