gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:04:04 25 4
gpt4 key购买 nike

当我想显示 image1 到 image6 时,我在上一个按钮上遇到错误,它显示错误 ArrayIndexOutOfBoundsException: length=6;指数=-1如何设置当上一个按钮来到 image1 时不尝试显示 image 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]);
}

最佳答案

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

关于android - 下一个/上一个按钮幻灯片图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com