1
我有一個活動片段,其中有一個YouTubeThumbnailView
。該視圖是從一個異步任務設置:泄漏並釋放YouTubeThumbnailLoaders
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 [email protected] that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.example.app.MyActivity has leaked ServiceConnection [email protected] that was originally bound here
我以爲這曾與在文檔中發現的有關何時與他們所做的釋放YouTubeThumbnailLoader
S上的警告做。正如在其他的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);
}
如何正確釋放加載器來避免這個問題?