OutOfMemoryError:位图大小超出 VM 预算(不是图像大小问题)

发布于 2024-11-26 22:42:20 字数 663 浏览 3 评论 0原文

我知道这个错误在 stackoverflow 上发生过很多次,但通常是位图大小问题。我的不一样。我有一个带有自定义适配器的 ListView。这是我的 getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }
    if (position % 2 == 0) {
        convertView.setBackgroundResource(R.drawable.cell_even_bg);
    } else {
        convertView.setBackgroundResource(R.drawable.cell_odd_bg);
    }
}

现在,当我有 for ex.我的 listView 中有 100 个单元格,我滚动此列表一段时间,最后我总是收到错误 OutOfMemoryError: 位图大小超出 VM 预算 - 在 DDMS 中,我可以看到它在调用 ConvertView.setBackgroundResource(...) 时发生。我的绘图尺寸为 1x10px,并在 Y 轴上重复。

I know this error occurred many times on stackoverflow, but usually it was bitmap size problem. Mine is diffrent. I have a ListView with custom adapter. Here's my getView method:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.cell, null);
    }
    if (position % 2 == 0) {
        convertView.setBackgroundResource(R.drawable.cell_even_bg);
    } else {
        convertView.setBackgroundResource(R.drawable.cell_odd_bg);
    }
}

Now, when I have for ex. 100 cells in my listView and I scroll this list for some time, finally I always get an error OutOfMemoryError: bitmap size exceeds VM budget - and in DDMS I can see that it happens while convertView.setBackgroundResource(...) is called. My drawables are 1x10px and repeated in Y-axis.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-12-03 22:42:20

我编写了一个包含 100 个单元格的测试应用程序来验证您的问题,发现它工作正常(滚动多次),没有 OOM 错误。
您的代码中一定存在其他内存泄漏。

I write a test application with 100 cells to verify your issue and found it works fine (scroll for many times), without OOM error.
There must be other memory leaks in your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文