我在YouTubePlayerActivity中使用片段中的youtube播放器
我在使用它时遇到这种错误:
广告是强制性的吗?
如何防止此错误?
(错误不会导致应用程序崩溃)
但是,当我多次快速打开和关闭youtube视频活动时,该应用会崩溃。 java.lang.IllegalStateException:YouTubeServiceEntity未初始化
public class YouTubePlayerFragment extends Fragment {
private YouTubePlayerSupportFragment mYouTubePlayerFragment;
private YouTubePlayer mYouTubePlayer;
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_youtube_player, container, false);
mYouTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
mYouTubePlayerFragment.initialize(VideoService.API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
mYouTubePlayer = player;
mYouTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
mYouTubePlayer.loadVideo(mVideoID);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(getActivity(), error, Toast.LENGTH_LONG).show();
}
}
});
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, mYouTubePlayerFragment).commit();
return view;
}
...
}
感谢您的帮助!