java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230


近期遇到了如標題這種錯誤,再次記錄解決方法。本文參考帖子:

http://bbs.csdn.net/topics/390196217

出現此bug的原因是在內存回收上。里面用Bitamp的代碼為:

top=(ImageView)view.findViewById(R.id.top);
		bitmap=ImgBitmap.comeFromRes(getResources(), R.drawable. top);
		top.setImageBitmap(bitmap);
		bottom=(ImageView)view.findViewById(R.id.bottom);
		bitmap1=ImgBitmap.comeFromRes(getResources(), R.drawable. bottom);
		bottom.setImageBitmap(bitmap1);

在回收時,應該這樣寫:




@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		top.setImageBitmap(null);
		bottom.setImageBitmap(null);
		ImgBitmap.MyRecycle(bitmap);
		ImgBitmap.MyRecycle(bitmap1);
		super.onDestroy();
		
			
		
		
	}

或者

@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		ImgBitmap.MyRecycle(mBitmap);
		ImgBitmap.MyRecycle(mBitmap1);
		top=null;
		bottom=null;
		
		super.onDestroy();
		
			
		
		
	}

另外MyRecycle方法的代碼例如以下:
public static void MyRecycle(Bitmap bmp){
		if(!bmp.isRecycled() && null!=bmp){
				bmp=null;
		}
		}
	}
	
總之是必需要 解除與bitmap有關的全部綁定。

轉載請注明出處:http://blog.csdn.net/android_jiangjun/article/details/39698459





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM