0

when i swipe images of animal or car,it give me error as i swipe from 3rd image to 4th image ,error is of arrayindexoutofboundexception ,i have seen tutorials but didnot find any specific answer related to my problem.I have posted in stackoverflow for the very first time so please try to understand my question ,your help would be appreciated thankyou

//Activity class which contain viewpager
package com.example.image;

public class LikeMainActivity extends FragmentActivity {
ViewPager vp;
fragment1 f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_like_main);
    vp=(ViewPager)findViewById(R.id.viewPager);

    f=new fragment1(getApplicationContext());
    vp.setCurrentItem(1);
    vp.setAdapter(f);
    }

//class named as fragment extends pager adapter

public class fragment1 extends PagerAdapter {
Context ctx;
LayoutInflater lytifltr;
private int[] bike = { R.drawable.bike, R.drawable.bike1, R.drawable.bike2, R.drawable.bike3, R.drawable.bike4,
        R.drawable.bike5 };
private int[] car = { R.drawable.car, R.drawable.car1, R.drawable.car2, R.drawable.car3 };
private int[] animal = { R.drawable.animal, R.drawable.animal1, R.drawable.animal2, R.drawable.animal3 };
private int[] flower = { R.drawable.flower, R.drawable.flower1, R.drawable.flower2, R.drawable.flower3,
        R.drawable.flower4 };
member m = new member();

public fragment1(Context ctx) {
    Intent i = new Intent();
    this.ctx = ctx;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return bike.length;
}

String abc = "";

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub

    return (arg0 == (LinearLayout) arg1);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    // TODO Auto-generated method stub
    lytifltr = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemview = lytifltr.inflate(R.layout.imageswipe, container, false);
    ImageView img = (ImageView) itemview.findViewById(R.id.imageVew1);
    abc = m.type.toString();
    if (m.type == "car") {
        img.setImageResource(car[position]);
    } else if (m.type == "flower") {
        img.setImageResource(flower[position]);
    } else if (m.type == "bike") {
        img.setImageResource(bike[position]);
    } else if (m.type == "animal") {

            img.setImageResource(animal[position]);

        }
    else
    {
        Toast.makeText(ctx, "Nothing", Toast.LENGTH_LONG).show();
    }
    container.addView(itemview);
    return itemview;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // TODO Auto-generated method stub
    container.removeView((LinearLayout) object);

}

@Override
public int getItemPosition(Object object) {
    // TODO Auto-generated method stub
    return super.getItemPosition(object);
}

}

Talha Asif
  • 35
  • 4

1 Answers1

1

The crash is because you are returning bike.length from getCount()

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return bike.length;
}

But accessing invalid index from instantiateItem(). (NB: I did fix your code to use equals() to compare strings, see this question for more explanations)

if ("car".equals(m.type)) {
    img.setImageResource(car[position]);
} else if ("flower".equals(m.type)) {
    img.setImageResource(flower[position]);
} else if ("bike".equals(m.type)) {
    img.setImageResource(bike[position]);
} else if ("animal".equals(m.type)) {
    img.setImageResource(animal[position]);
}

The number of items in bike,car, animal, flower array are different:

private int[] bike = { R.drawable.bike, R.drawable.bike1, R.drawable.bike2, R.drawable.bike3, R.drawable.bike4,
        R.drawable.bike5 }; //Total 6
private int[] car = { R.drawable.car, R.drawable.car1, R.drawable.car2, R.drawable.car3 }; //Total 4
private int[] animal = { R.drawable.animal, R.drawable.animal1, R.drawable.animal2, R.drawable.animal3 }; //Total 4
private int[] flower = { R.drawable.flower, R.drawable.flower1, R.drawable.flower2, R.drawable.flower3,
        R.drawable.flower4 }; //Total 5

And hence when you try to access lets say 5th page, and number of elements in animal array is only 4, you get an ArrayIndexOutOfBoundException

Solution:

You have to make all the arrays contain equal number of elements or in getCount() return that array length which has minimum number of elements

Community
  • 1
  • 1
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45