java - 泄露并发布 YouTubeThumbnailLoaders

标签 java android youtube-api

我有一个 Activity fragment ,其中包含一个 YouTubeThumbnailView。该 View 是从异步任务设置的:

public class MyFragment extends Fragment {

  private YouTubeThumbnailView mThumbnailView;

  public class FetchThumbnailTask extends AsyncTask<> implements YouTubeThumbnailView.OnInitializedListener {

    protected void onPostExecute(String videoId) {
        mThumbnailView.setTag(videoId);
        mThumbnailView.initialize(YOUTUBE_DATA_API_KEY, this);
    }

    @Override
    public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
        loader.setVideo((String) view.getTag());
    }
  }
}

当我为缩略图中的视频启动(或有时仅在点击后退按钮时)YouTubeStandalonePlayer 时(单击缩略图时),这会导致出现以下错误:

E/ActivityThread: Activity com.example.app.MyActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@1cc07092 that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.app.MyActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@1cc07092 that was originally bound here

我认为这与文档中发现的有关在使用完 YouTubeThumbnailLoader 后释放它们的警告有关。正如其他 StackOverflow 问题中所建议的,我尝试将加载器保存为 onInitializationSuccess 中的 MyFragment 中的 mThumbnailLoader 并在点击操作中释放它:

@Override
public void onInitializationSuccess(YouTubeThumbnailView view, YouTubeThumbnailLoader loader) {
    loader.setVideo((String) view.getTag());
    mThumbnailLoader = loader;
}
@Override
public void onClick(View v) {
    if (mThumbnailLoader != null) {
        mThumbnailLoader.release();
    }

    Intent intent = YouTubeStandalonePlayer.createVideoIntent(getActivity(), YOUTUBE_DATA_API_KEY, (String) v.getTag());
    startActivity(intent);
}

如何正确释放加载程序以避免此问题?

最佳答案

此问题似乎是由 Activity (以及 fragment )被点击事件以外的其他事件破坏引起的。只需在 onDestory() 中重复 mThumbnailLoader.release(); block 即可解决该问题。

此外,实际崩溃是由另一个问题引起的。就其本身而言,泄漏的 ServiceConnection 错误仅在日志中可见,不会对应用产生任何面向用户的影响。

关于java - 泄露并发布 YouTubeThumbnailLoaders,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37780082/

相关文章:

android - 使用 Google Maps API 手动调整路线

android - 在不启动的情况下在Android中重置动画

javascript - setPlaybackQuality 不起作用 YouTube JS API

.net - 使用youtube API检索youtube视频的视频分辨率(质量)是多少?

java - 我创建的每个main()函数上的ArrayIndexOutOfBoundsException

java - 如何读写SD卡?

javascript - 仅接收最后发送的消息(Firebase 云消息传递)

Youtube - 如何浏览给定类别中的所有 channel ?

java - Dispose() 不适用于每一帧

java - Jsch 或 SSHJ 或 Ganymed SSH-2?