Android 的 logcat
的输出通知我,在我的程序中的某个时刻抛出了 ArrayIndexOutOfBoundsException
:
我之前为按钮设置了一个 boolean 值,该按钮用于测试目的。
这是认为错误所在的代码:
int hour = new Date().getHours();
int min = new Date().getMinutes();
int secs = new Date().getSeconds();
String date = hour + ":" + min+ ":" + secs;
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bSet = true;
}
});
while (date =="01:00:00" || bSet == true);
int randomNumber = rand.nextInt();
tv.setText(getResources().getString(ids[randomNumber]));
最佳答案
我认为你在最后一行有异常(exception)。
就在这里 ids[randomNumber]
您应该检查随机生成的数字,因为它可能大于 ids.length。
使用rand.nextInt(ids.length);
它将生成从0到ids.length的随机数。
关于java - 我的代码中出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21220808/