所以我并没有真正得到这个错误消息右边的理由。我不确定为什么会发生这种情况,但我的猜测是这与大约有 50 个高质量的可绘制对象有关。在真正快速滚动时,应用程序崩溃。我觉得好像我正在缓解 ListView 和崩溃的最常见问题,例如使用 View Holders 以及只启动一次充气机。
Process: com.example.michael.myandroidappactivity, PID: 20103
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
这是代码
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<Integer> imageIds;
private static LayoutInflater inflater;
public ImageAdapter(Context _context, ArrayList<Integer> _imageIds)
{
context = _context;
imageIds = _imageIds;
}
@Override
public int getCount()
{
return imageIds.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
static class ViewHolder{
ImageView img;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
View rowView = null;
if(rowView==null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.listview_layout, parent, false);
holder = new ViewHolder();
holder.img = (ImageView) rowView.findViewById(R.id.flag);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.img.setImageResource(imageIds.get(position));
return rowView;
}
}