Android InvalidStateException on ViewPager when setting page limit too small

116 views Asked by At

I have a problem with my ViewPager Image slider gallery. I am setting my page limit to 3 but then my app craches on the third image because the child has already a parent. I set the limit to this number because I want to avoid large memory leaks, event with 3 images it uses up like 150Mb of allocated memory which is incredibly huge ammount. Maybe if I resize the image before uploading I can get them to work with more pages ? Anyways, here is my code for the viewPager:

ViewPager mViewPager = (ViewPager) findViewById(R.id.imagepager);
    ViewerAdapter adapter = new ViewerAdapter(this);
    //adapter.notifyDataSetChanged();
    mViewPager.setAdapter(adapter);
    mViewPager.setOffscreenPageLimit(3);
    mViewPager.setCurrentItem(number);
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            time.setText(caluculateTimeAgo(MainActivity.time[position]));
            if (MainActivity.distances[position] < 1000) {
                dist.setText(String.valueOf(MainActivity.distances[position]) + "m");
            } else {
                long f = (long) (MainActivity.distances[position] / 1000);
                dist.setText(String.valueOf(f) + "km");
            }
            author.setText(MainActivity.users[position]);
            text.setText(MainActivity.text1[position]+MainActivity.tags[position]);
        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

and here is my adapter definition:

public class ViewerAdapter extends PagerAdapter {
    Context mContext;
    public ViewerAdapter(Context context) {
        mContext = context;
        mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    LayoutInflater mLayoutInflater;
    @Override
    public int getCount() {
        return MainActivity.imgLink.length;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == (View) arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        Picasso.with(container.getContext()).cancelRequest((ImageView) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return (position + "/" + getCount());
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
        final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
                Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
                startActivity(intent);
            }
        });
        Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).into(photo);
        container.addView(photo);
        return photo;
    }
}
1

There are 1 answers

0
Lukas Anda On

Well, I managed to make a work-around for this problem. I forgot to set the cropping of the photo and therefore the photos were too large for this. And also, I set the offScreenPageLimit to 30 to avoid this error (This is that work-around because with large quantity of image it will come to out of memory error) So now, here is my adapter code:

@Override
    public Object instantiateItem(ViewGroup container, int position) {

        final View itemView = mLayoutInflater.inflate(R.layout.pager_item,container,false);
        final ImageView photo = (ImageView) itemView.findViewById(R.id.photoSlide);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fullscreen = ((BitmapDrawable)photo.getDrawable()).getBitmap();
                Intent intent = new Intent(ShowMarkerContent.this, FullScreenImageActivity.class);
                startActivity(intent);
            }
        });
        Picasso.with(container.getContext()).load("MyWebsite" + MainActivity.imgLink[position]).fit().centerCrop().into(photo);
        container.addView(itemView);
        return itemView;
    }