21
I'm working on a gamebook application that shows 12 views in a ViewPager. This is my custom PagerAdapter :
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] { R.drawable.copertinai,
R.drawable.blui, R.drawable.azzurroi, R.drawable.rossoi,
R.drawable.gialloi, R.drawable.verdei, R.drawable.rosai,
R.drawable.grigioi, R.drawable.neroi, R.drawable.arancionei,
R.drawable.marronei, R.drawable.violai, R.drawable.ulm };
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = MainActivity.this;
RelativeLayout relLayImageView = new RelativeLayout(context);
relLayImageView.setBackgroundResource(mImages[position]);
((ViewPager) container).addView(relLayImageView, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return relLayImageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
object=null;
System.gc();
}
}
In some devices it cause an out of memory exceptionr randomly when this line of code is called
relLayImageView.setBackgroundResource(mImages[position]);
But in all devices I see something like this in the logcat when i turn pages:
12-31 00:25:31.655: I/dalvikvm-heap(9767): Grow heap (frag case) to 50.875MB for 10384016-byte allocation
The app also crashes in some devices for the same problem when in another Activity I set different background resource to the main layout based on user action. Here the