首先,我已经在这里的一些线程中阅读了此问题,并且我知道用应用程序上下文替换getActivity()可以解决。所有这些答案都来自几年前,现在我认为它不支持应用程序上下文,因为在放置应用程序上下文时,在库的方法中出现错误,找不到任何方法可以放置应用程序上下文。
当我创建YoutubeStandalonePlayer时,我正在这样做:
Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), getResources().getString(google_maps_key), mPublication.getYoutubeCode());
如果尝试放置应用程序上下文,则会收到错误消息,因为我传递的是应用程序上下文,而不是 Activity ,即方法正在等待的属性。
Intent intent = YouTubeStandalonePlayer.createVideoIntent(ApplicationConfig.getAppContext(), getResources().getString(google_maps_key), mPublication.getYoutubeCode());
然后,我的问题是...如何使用YouTubeStandalonePlayer解决ServiceConnectionLeaked问题:
android.app.ServiceConnectionLeaked: Activity com.buzinger.loycus.activity.HomeActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@cbfec60 that was originally bound here
提前致谢
最佳答案
尝试在此网站(https://androidadagnitio.wordpress.com/2017/03/09/activity-has-leaked-serviceconnection-com-google-android-youtube-player-internal-re391c339-that-was-originally-bound-here-error-solution/)中找到的此解决方案
您需要添加此行以避免youtube api出现ServiceConnectionLeaked。
youTubeThumbnailLoader.release();
所有代码:
@Override
public void onBindViewHolder(final VideoInfoHolder holder,final int position) {
holder.youTubeThumbnailView.initialize(DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(videos.get(position));
//here is the magic to solve the logcat error
youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
youTubeThumbnailView.setVisibility(View.VISIBLE);
holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
youTubeThumbnailLoader.release();
}
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
}
});
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
//write something for failure
}
});
}