1. 程式人生 > >Canvas: trying to use a recycled bitmap

Canvas: trying to use a recycled bitmap 阿新 發佈:2018-12-31

java.lang.RuntimeException: Canvas: 
trying to use a recycled bitmap [email protected]
                                                                                      at android.graphics.BaseCanvas.throwIfCannotDraw(BaseCanvas.java:55)
                                                                                      at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:226)
                                                                                      at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97)
                                                                                      at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529)
                                                                                      at android.view.View.getDrawableRenderNode(View.java:19377)
                                                                                      at android.view.View.drawBackground(View.java:19313)
                                                                                      at android.view.View.draw(View.java:19110)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18069)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
                                                                                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18028)
                                                                                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
                                                                                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18028)
                                                                                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4198)
                                                                                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4178)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18028)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18060)
                                                                                      at android.view.View.draw(View.java:18847)
                                                                                      at android.view.ViewGroup.drawChild(ViewGroup.java:4214)
                                                                                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4000)
                                                                                      at android.view.View.draw(View.java:19122)
                                                                                      at com.android.internal.policy.DecorView.draw(DecorView.java:785)
                                                                                      at android.view.View.updateDisplayListIfDirty(View.java:18069)
                                                                                      at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:643)
                                                                                      at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:649)
                                                                                      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:757)
                                                                                      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2980)
                                                                                      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2794)
                                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2347)
                                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
                                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
                                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
                                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:723)
                                                                                      at android.view.Choreographer.doFrame(Choreographer.java:658)
                                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
                                                                                      at android.os.Handler.handleCallback(Handler.java:789)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                      at android.os.Looper.loop(Looper.java:164)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
08-13 06:44:54.834 21842-21842/com.picart.snapfilter.squareblur E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

解決:

以上兩篇建議新建ImageView,重寫onDraw()

報錯: drawBitmap catch some exceptions

失敗

這篇建議註釋掉onDestroy

失敗

這篇建議定位使用了已回收圖片的地方,直接註釋掉

這篇worked,but I didn't locate the error position sucessfully

我怎麼找到的呢:找我最近改動的幾個java檔案,command+F:recycle,然後註釋掉,執行一下,發現確實可以解決這個error。

但是還存在一個問題,我寫的listview是很多張圖片,在我點選圖1,然後點選圖2,……,然後點選圖6,再返回點選圖2時候,肉眼可以看見背景變化過程中有圖1的快取,雖然這個變化過程很快但是我看見了……

出錯的在這裡
public void recycleDrawable(Drawable drawable) {
		if (drawable instanceof BitmapDrawable) {
			Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
			if (bmp != null && !bmp.isRecycled()) {
//				bmp.recycle();//必須註釋掉
				bmp = null;
			}
		}
	}