I am new to android and java. I made an application which works fine except that i get java.lang.OutOfMemoryError exception when i scroll down the listviews in my app which increase dynamically (10 items) every time i scroll to the bottom. after scrolling some 4-5 pages i get this exception. can any one hep me solve this exception and some tips to avoid such exceptions.
My Adapter:
public class LazyAdapter extends BaseAdapter{
private static LayoutInflater inflater = null;
private Activity activity;
public ImageLoader imageLoader;
public static ArrayList<Product> values;
private String product_action;
public LazyAdapter(Activity a, ArrayList<Product> arg3) {
values = arg3;
activity = a;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return values.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView text;
public ImageView image;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
Product p = values.get(position);
if (convertView == null) {
vi = inflater.inflate(R.layout.feed_items, null);
holder = new ViewHolder();
holder.text = (TextView) vi.findViewById(R.id.label);
;
holder.image = (ImageView) vi.findViewById(R.id.logo);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (condition1) {
holder.text.setText(myText);
holder.image.setTag(imageUrl);
imageLoader.DisplayImage(imgURL, activity, holder.image);
} else if (condition2) {
holder.text.setText(myText);
holder.image.setTag(imageUrl);
imageLoader.DisplayImage(imgURL, activity, holder.image);
} else if (condition3) {
holder.text.setText(myText);
holder.image.setTag(imageUrl);
imageLoader.DisplayImage(imgURL), activity,
holder.image);
}
return vi;
}
public void add(Product product) {
values.add(product);
}
}
java.lang.outofmemmoryerror occur when dvm have no memory space. This is the issue of memory leaks. A memory leak happens when the application keeps more and more references to objects and never releases them. The garbage collector will therefore never collect those objects and less and less free memory will be available until we reach the point where not enough free memory is available for the application to function normally.Thrown when a request for memory is made that can not be satisfied using the available platform resources. Such a request may be made by both the running application or by an internal function of the VM. one thing more every time you scroll the list then vm needs memory to show that list so that to free the list object with previous view.
To fix this, first create an XML file in which you create a
ListViewlike this (Xmlfilename isyourlist.xml):Now create an XML in which your
ImageViewandTextViewis created. (XML file name:yourimagetextlayout.xml):Now you have your
Activityclass where you can find yourListViewby its id and create a custom adapter class in which you can fill your list like this:Now in your
Activityclass find yourListViewby its id:Just instantiate the adapter class like this:
and add this adapter into your ListView: