gpt4 book ai didi

android - Canvas : trying to use a recycled bitmap android. graphics.Bitmap

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:07:03 72 4
gpt4 key购买 nike

11-24 23:19:18.434: ERROR/AndroidRuntime(12660): Uncaught handler: thread main exiting due to uncaught exception
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@4384c218
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.graphics.Canvas.throwIfRecycled(Canvas.java:955)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.graphics.Canvas.drawBitmap(Canvas.java:1044)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:291)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.ImageView.onDraw(ImageView.java:908)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6283)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1581)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1579)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6286)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1581)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.AbsListView.dispatchDraw(AbsListView.java:1323)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.ListView.dispatchDraw(ListView.java:2933)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6389)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.AbsListView.draw(AbsListView.java:2142)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1581)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1579)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6286)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1581)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6286)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.drawChild(ViewGroup.java:1581)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1311)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.View.draw(View.java:6286)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.widget.FrameLayout.draw(FrameLayout.java:352)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1928)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewRoot.draw(ViewRoot.java:1454)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewRoot.performTraversals(ViewRoot.java:1174)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.view.ViewRoot.handleMessage(ViewRoot.java:1774)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.os.Looper.loop(Looper.java:123)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at android.app.ActivityThread.main(ActivityThread.java:4321)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at java.lang.reflect.Method.invoke(Method.java:521)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
11-24 23:19:18.684: ERROR/AndroidRuntime(12660): at dalvik.system.NativeStart.main(Native Method)

我使用 SoftReference 实现了图像缓存,并使用图像缓存中的位图设置了 ImageView。

在将位图设置为 ImageView 之前,我不使用 Bitmap.recycle() 并检查 Bitmap.isRcycled()。

我不明白为什么 Canvas 是用回收的位图绘制的。它很少发生,但确实发生过。

有什么建议吗?谢谢!

最佳答案

如果有人来到这里并且正在努力弄清楚哪个位图被回收了(就像我一样),下面是我最终解决这个问题的方法:
1) 我放置了一个全局的 Thread.setDefaultUncaughtExceptionHandler 来转储堆(实际上我已经有了它来诊断 OOM 错误)。处理程序中的代码是这样的:

File f = new File(Environment.getExternalStorageDirectory(),"oom-error.hprof");
String path = f.getAbsolutePath();
// force a few GC before dumping stuff
System.gc();
System.gc();
Debug.dumpHprofData(path);

2) 使用 this很棒的文章,您可以学习如何使用 MAT 加载和分析 hprof 转储文件.

3) 在 MAT 内部,我首先通过“Bitmap”正则表达式过滤类,然后查找 Bitmap 引用 ID(在 OP 案例中:“4384c218”)。这会让你很清楚哪个 View 正在保存这个回收的位图,你可以开始考虑它的解决方案。(在我的例子中,我从一个公共(public)缓存中回收图像,其中一些是可绘制对象,它们也在 XML 布局中使用,并且应该在 OnDestroy 之前“固定”到缓存中)。

关于android - Canvas : trying to use a recycled bitmap android. graphics.Bitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4268471/

72 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com