我只是在我的一个标签片段中使用了YoutubePlayer,但无法集成它!
使用此代码,它给了我此错误。
Binary XML file line #0: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class com.google.android.youtube.player.YouTubePlayerView
这是我的片段类。
public class Frag_Trailer extends Fragment {
YouTubePlayerView mYouTubePlayerView;
YouTubePlayer.OnInitializedListener mOnInitializatedListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_trailer, container, false);
mYouTubePlayerView = (YouTubePlayerView)rootView.findViewById(R.id.view_youtube);
mYouTubePlayerView.initialize(YouTubeConfig.getYoutubeApiKey(), mOnInitializatedListener);
mOnInitializatedListener = new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("_XgQA9Ab0Gw");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
return rootView;
}
}
这是我在build.gradle
中的依赖项。
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:cardview-v7:23.3.0'
implementation 'com.android.support:design:23.3.0'
implementation files('libs/YouTubeAndroidPlayerApi.jar')
}
基于
这个答案
您必须使用 YouTubeBaseActivity
扩展您的活动。但是在您的情况下,您可以在片段上实现它,因此请像这样更改视图 XML
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtubesupportfragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>