2015-11-03 75 views
0

我試圖讓雙查看傳呼機,我從我的PagerAdapter代碼重寫destroyItem功能就像波紋管:unbindDrawables在PagerAdapter運行Android Lollipop不工作

@Override 
public void destroyItem(ViewGroup container, int position, Object object) 
{ 
    container.removeView((View) object); 
    unbindDrawables((View) object); 
    System.gc(); 
    object = null; 
} 

protected void unbindDrawables(View view) 
{ 
    if (view instanceof ImageView) 
    { 
     Drawable drawable = ((ImageView) view).getDrawable(); 
     if (drawable instanceof BitmapDrawable) { 
      BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; 
      Bitmap bitmap = bitmapDrawable.getBitmap(); 
      bitmap.recycle(); 
     } 
     ImageWorker.cancelWork(((ImageView) view)); 
     ((ImageView) view).setImageResource(0); 
     ((ImageView) view).setImageDrawable(null); 
    } 
    if (view.getBackground() != null) 
    { 
     view.getBackground().setCallback(null); 
    } 
    if (view instanceof ViewGroup) 
    { 
     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) 
     { 
      unbindDrawables(((ViewGroup) view).getChildAt(i)); 
     } 
     if (!(view instanceof AdapterView<?>)) 
     { 
      ((ViewGroup) view).removeAllViews(); 
     } 
     //((ViewGroup) view).removeAllViews(); 
    } 
} 

一切OK Android KitKat上,果凍豆,甚至在薑餅和冰淇淋三明治上,但是當我嘗試在API 21和更高版本上測試我的應用程序時,我有內存異常。當我調試我的代碼時,我看不到問題。誰能幫我 ?謝謝。

回答

0

我從代碼中看不到從視圖層次結構中刪除imageview的代碼,如果在imageView上調用了unbindDrawables而不是viewgroup,那麼它不會工作以移除imageview位圖泄漏。我有一個類似的問題,內存不足,結果是ImageView阻止位圖的字節[]的GC,唯一的解決方案是從視圖層次結構中刪除ImageView(並丟棄引用)。這是不夠的,我做了以下清理

if (imageView.getBackground() != null) { 
     imageView.getBackground().setCallback(null); 
    } 
    setImageBackground(imageView, null); 
    imageView.setImageBitmap(null); 
    imageView.setImageDrawable(null); 

我也有很多采用Android Studio的內存分析其對這一問題的關注周圍的問題。

Leaked unreferenced byte[] originally from bitmap but recycled() causing memory-leak (until activity stopped)

相關問題