android 如何分析java.lang.IllegalArgumentException: Cannot draw recycled bitmaps异常

时间:2023-12-22 12:52:44

这类问题的分析,通常你需要找到bitmap对象已经在那个位置recyle,然后检查代码。

如何定位的位置,其中代码具有对bitmap 目的recyle。能够 Bitmap.java的recycle方法,加log打印call stack要找到。

 

详细的改动參考例如以下:

 

 public void recycle() {

        if (!mRecycled) {

            if (nativeRecycle(mNativeBitmap)) {

                // return value indicates whether native pixel object was actually recycled.

                // false indicates that it is still in use at the native level and these

                // objects should not be collected now. They will be collected later when the

                // Bitmap itself is collected.

                mBuffer = null;

                mNinePatchChunk = null;

            }

            mRecycled = true;

           

            log.e("bitmap recyle! ", "this = " + this ,new Throwable("recycle"));





        }

    }

 

然后在抛 java.lang.IllegalArgumentException: Cannot draw recycled bitmaps 异常的地方,也将bitmap 对象打印出来。 抓取复现问题的mobile log 分析。

比如例如以下的log:

 

在main log中搜索“FATAL EXCEPTION”看到发生fatal error exception的 trace例如以下。相应的bitmap对象为 android.graphics.Bitmap@4218d0c8

01-01 08:03:23.841  2369  2369 D AndroidRuntime: Shutting down VM

01-01 08:03:23.841  2369  2369 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40f979a8)

01-01 08:03:23.856  2369  2369 E AndroidRuntime: FATAL EXCEPTION: main

01-01 08:03:23.856  2369  2369 E AndroidRuntime: java.lang.IllegalArgumentException: Cannot draw recycled bitmapsandroid.graphics.Bitmap@4218d0c8

然后再在main log中搜索keyword “@4218d0c8” ,就能够看到该对象被recyle的call stack

 

01-01 08:03:22.741  2369  2369 E bitmap recyle! : this = android.graphics.Bitmap@4218d0c8

01-01 08:03:22.741  2369  2369 E bitmap recyle! : java.lang.Throwable: recycle

01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at android.graphics.Bitmap.recycle(Bitmap.java:214)

01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at com.xxx.xxxx.xxxActivity.onDestroy(xxxActivity.java:190)

01-01 08:03:22.741  2369  2369 E bitmap recyle! :  at android.app.Fragment.performDestroy(Fragment.java:1908)

......