在我的应用程序中,我想在单击下一步和返回按钮时左右导航并更改 textView 值。但是当索引大于项目数时,我会收到强制关闭错误。这是我的代码。
String[] seperated = imag;
count_image = seperated.length-1;
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new ImagePagerAdapter(seperated));
pager.setCurrentItem(pagerPosition);
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.ic_launcher)
.cacheOnDisc()
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
.build();
pager.setOffscreenPageLimit(3);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.boutton_slide_droite:
focus_image = pager.getCurrentItem();
//get current image index
focus_image++;
//move to next record
position++;
System.out.println("focus_image_up:"+focus_image);
if(focus_image<count_image){
if(focus_image>=count_image){
focus_image=0;
pagerPosition=0;
}else {
pager.setCurrentItem(focus_image);
disable_progress=true;
System.out.println("focus_image up if ++:
"+focus_image);
text_num.setText("No. "+PostList.get(focus_image).getNum());
text_name.setText(PostList.get(focus_image).getNom());
text_team.setText(PostList.get(focus_image).getTeam());
text_date.setText(PostList.get(focus_image).getNaissance());
if
(PostList.get(position).getNBVictoire().equals("1"))
text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());
else
text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());
if (PostList.get(focus_image).getNBCourse().equals("1"))
text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());
else
text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());
}
}else {
pagerPosition=count_image;
focus_image=count_image;
}
break;
case R.id.boutton_slide_gauche:
focus_image = pager.getCurrentItem();
focus_image--;
//move to previous record
position--;
if (focus_image>0){
if (focus_image<=count_image) {
System.out.println("focus_image_down: if "+focus_image);
pager.setCurrentItem(focus_image);
disable_progress=true;
text_num.setText("No. "+PostList.get(focus_image).getNum());
text_name.setText(PostList.get(focus_image).getNom());
text_team.setText(PostList.get(focus_image).getTeam());
text_date.setText(PostList.get(focus_image).getNaissance());
if (PostList.get(focus_image).getNBVictoire().equals("1"))
text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());
else
text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());
if (PostList.get(position).getNBCourse().equals("1"))
text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());
else
text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());
}else {
pager.setCurrentItem(focus_image);
disable_progress=true;
text_num.setText("No. "+PostList.get(focus_image).getNum());
text_name.setText(PostList.get(focus_image).getNom());
text_team.setText(PostList.get(focus_image).getTeam());
text_date.setText(PostList.get(focus_image).getNaissance());
if
(PostList.get(focus_image).getNBVictoire().equals("1"))
text_victoires.setText("Victoire: "+PostList.get(focus_image).getNBVictoire());
else
text_victoires.setText("Victoires: "+ PostList.get(focus_image).getNBVictoire());
if
(PostList.get(position).getNBCourse().equals("1"))
text_nomb.setText("Nombre de course: "+PostList.get(focus_image).getNBCourse());
else
text_nomb.setText("Nombre de courses: "+PostList.get(focus_image).getNBCourse());
}
}else {
focus_image=0;
pagerPosition=0;
}
break;
}
错误是
java.lang.IndexOutOfBoundsException:索引 3 无效,大小为 3