我的应用程序中出现 ArrayIndexOutofBounds 异常。我想在我的应用程序中添加一个上一个按钮。我的数组中有 5 个元素,按钮循环遍历。如何做到当用户点击 element[0] 时,它将循环回 element[4] 而不是转到导致异常的 [-1]?
我的代码如下:
mPrevButton = (ImageButton)findViewById(R.id.prev_button);
mPrevButton.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
}
执行此操作的正确方法是什么:
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
最佳答案
保持简单吗?
if (mCurrentIndex > 0)
mCurrentIndex--;
else
mCurrentIndex = mQuestionBank.length-1;
关于java - Android 应用程序中的 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16870680/