下一个/上一个按钮幻灯片图像

时间:2014-01-08 11:46:34

标签: android image slide

当我想将image1显示到image6时,我在上一个按钮上收到错误它显示错误 ArrayIndexOutOfBoundsException:length = 6;指数= -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]);
}

}

1 个答案:

答案 0 :(得分:4)

mNekiPhotoIndex = ((mNekiPhotoIndex - 1) >= 0 ? (mNekiPhotoIndex - 1) : 0) % mSlikeID.length;