标签式活动中的 Youtube Player 导致此错误 [英] Youtube Player Inside Tabbed Activity Cause This Error
问题描述
我有标签式活动,我想在第三个标签上播放 YouTube 视频.
I have tabbed activity and I want to have youtube video on third tab.
这是我的 java :
here's my 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 :
and here's my 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;
}
}
}
当我尝试运行此代码时,出现以下错误:
when I tried to run this code, I got this following error :
Error:(80, 28) error: incompatible types: FragmentTutorial cannot be converted to Fragment
我在这里错过了什么?谢谢...
what did I missed here? thank you...
推荐答案
可能是Fragment类错了,YouTubePlayerFragment
extends android.app.Fragment
, FragmentPagerAdapter
需要 android.support.v4.app.Fragment
并且无法转换
Probably it is caused by the wrong Fragment class, YouTubePlayerFragment
extends android.app.Fragment
, FragmentPagerAdapter
requires android.support.v4.app.Fragment
and this cannot be converted
尝试使用 android.support.v13.app.FragmentPagerAdapter
代替 android.support.v4.app.FragmentPagerAdapter
(其他片段应该扩展 android.app.片段
)
try to use android.support.v13.app.FragmentPagerAdapter
instead android.support.v4.app.FragmentPagerAdapter
(other fragments should extends android.app.Fragment
)
这篇关于标签式活动中的 Youtube Player 导致此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!