Android YouTubePlayer API - YouTubeService已泄露IntentReceiver - 无法获得广告ID

时间:2017-05-04 13:06:25

标签: java android memory-leaks android-youtube-api

我在YouTubePlayerActivity中使用片段中的youtube播放器

我在使用它时遇到这种错误:

  • 打开youtube播放器时
    • 无法获得广告ID java.lang.NullPointerException:尝试从空对象引用上的字段'java.lang.String iwq.a'中读取

广告是强制性的吗?

  • 关闭youtube播放器活动时
    • 服务com.google.android.youtube.api.service.YouTubeService已泄露最初在此处注册的IntentReceiver udp @ bf4a7a。你是否错过了对unregisterReceiver()的调用?

如何防止此错误?

(错误不会导致应用程序崩溃)

但是,当我多次快速打开和关闭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;
    }

    ...

}

感谢您的帮助!

0 个答案:

没有答案