OutOfMemoryError:位图大小超出 VM 预算(不是图像大小问题)
我知道这个错误在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我编写了一个包含 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.