2016-06-12 65 views
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); 
} 

如何正確釋放加載器來避免這個問題?

回答

0

此問題似乎是由於click事件以外的其他事件破壞了活動(並因此導致碎片)。簡單地重複mThumbnailLoader.release();onDestory()解決了問題。

此外,實際崩潰是由另一個問題引起的。在它自己泄漏的ServiceConnection錯誤只在日誌中可見,沒有任何用戶面臨的應用程序的影響。