ListView causing OutOfMemory Error
Posted
by
Michael
on Stack Overflow
See other posts from Stack Overflow
or by Michael
Published on 2014-08-24T03:20:01Z
Indexed on
2014/08/24
4:20 UTC
Read the original article
Hit count: 228
So I am not really given a reason to the right of this error message. I am not exactly sure why this is happening but my guess though is that it has to do with the fact that there are around ~50 good quality drawables. Upon scrolling really fast, the app crashes. I feel as if I am mitigating most common issues with ListView and crashing such as using View Holders as well as only initiating the inflater once.
Process: com.example.michael.myandroidappactivity, PID: 20103
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
Here is the code
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;
}
}
© Stack Overflow or respective owner