有关Bitmap使用时报错 Canvas: trying to use a recycled bitmap

操作步骤及现象:

应用程序在播放视频时点击Home键,应用程序被主页覆盖,运行于后台,再一次点击应用程序图标,程序崩溃,报错如下:

 FATAL EXCEPTION: main
 java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@41434a00
 	at android.graphics.Canvas.throwIfRecycled(Canvas.java:1038)
 	at android.graphics.Canvas.drawBitmap(Canvas.java:1142)
 	at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:393)
 	at android.widget.ImageView.onDraw(ImageView.java:892)
 	at android.view.View.draw(View.java:11180)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.View.draw(View.java:11183)
 	at android.widget.FrameLayout.draw(FrameLayout.java:450)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.View.draw(View.java:11183)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.View.draw(View.java:11183)
 	at android.widget.FrameLayout.draw(FrameLayout.java:450)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2895)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.ViewGroup.drawChild(ViewGroup.java:2893)
 	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2497)
 	at android.view.View.draw(View.java:11183)
 	at android.widget.FrameLayout.draw(FrameLayout.java:450)
 	at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2295)
 	at android.view.ViewRootImpl.draw(ViewRootImpl.java:2231)
 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1837)
 	at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2649)
 	at android.os.Handler.dispatchMessage(Handler.java:99)
 	at android.os.Looper.loop(Looper.java:137)
 	at android.app.ActivityThread.main(ActivityThread.java:4512)
 	at java.lang.reflect.Method.invokeNative(Native Method)
 	at java.lang.reflect.Method.invoke(Method.java:511)
 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
 	at dalvik.system.NativeStart.main(Native Method)

问题原因:

点击home键时执行Activity生命周期中的OnStop(),而我在这方法里面执行了释放代码:

		if (bitmap != null && (!bitmap.isRecycled()))
		{
			bitmap.recycle();
		}

于是再次进入此界面时就出现如上错误。




你可能感兴趣的:(有关Bitmap使用时报错 Canvas: trying to use a recycled bitmap)