调用已经recycle的bitmap出错
开发时,重新进入界面时报错如下:
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@5b2eab2
at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1270)
at android.graphics.Canvas.drawBitmap(Canvas.java:1404)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:544)
at android.widget.ImageView.onDraw(ImageView.java:1226)
at android.view.View.draw(View.java:16182)
at android.view.View.updateDisplayListIfDirty(View.java:15178)
at android.view.View.draw(View.java:15952)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16185)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2447)
at android.view.View.updateDisplayListIfDirty(View.java:15178)
at android.view.View.draw(View.java:15952)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16185)
at android.view.View.updateDisplayListIfDirty(View.java:15178)
at android.view.View.draw(View.java:15952)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15173)
at android.view.View.draw(View.java:15952)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15173)
at android.view.View.draw(View.java:15952)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16185)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2760)
at android.view.View.updateDisplayListIfDirty(View.java:15178)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2630)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2449)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2082)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1122)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6083)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5439)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:757)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:647)
查了一下代码,是由于调用了bitmap的recycle()方法,但是由于需要又不得不调用,经过搜索,得出如下解决这个Bug的方法:
调用recycle()方法后,将imageView的资源引用设置为null即可
imageView.setImageDrawable(null);
imageView.setImageBitmap(null);