1
我收到上一个按钮时出现错误,当我想要显示image1到image6它显示我的错误ArrayIndexOutOfBoundsException:length = 6; index = -1 如何设置前一个按钮来到image1不尝试显示图像6什么都不做?下一个/前一个按钮幻灯片图像
这里是我的代码
public class MainActivity extends Activity {
Intent i;
private int mNekiPhotoIndex =0;
private int[] mSlikeID = new int[] { R.drawable.pic,
R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.ic_launcher, };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showPhoto(mNekiPhotoIndex);
// 'Next' button.
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mNekiPhotoIndex = (mNekiPhotoIndex + 1)
% mSlikeID.length;
showPhoto(mNekiPhotoIndex);
}
});
// 'Prev' button.
Button prevButton = (Button) findViewById(R.id.prev_button);
prevButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mNekiPhotoIndex = (mNekiPhotoIndex - 1)
% mSlikeID.length;
showPhoto(mNekiPhotoIndex);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("photo_index", mNekiPhotoIndex);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
mNekiPhotoIndex = savedInstanceState.getInt("photo_index");
showPhoto(mNekiPhotoIndex);
super.onRestoreInstanceState(savedInstanceState);
}
private void showPhoto(int PhotoIndex) {
// TODO Auto-generated method stub
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(mSlikeID[PhotoIndex]);
}
}
它的工作原理谢谢 – ArmsOP