java.lang.ArrayIndexOutOfBoundsException:长度=10;指数=10
我已经开始开发有多项选择题的应用程序,但当我尝试运行我的应用程序时,它会说:java.lang.ArrayIndexOutOfBoundsException:长度=10;指数=10,java,android,indexoutofboundsexception,Java,Android,Indexoutofboundsexception,我已经开始开发有多项选择题的应用程序,但当我尝试运行我的应用程序时,它会说: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10 我知道它说什么,但我不明白问题出在哪里 这是我的密码: TextView tvq; InputStream is; String[] question = new String[10]; ImageButton[] buttons = new ImageButton[4]
java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
我知道它说什么,但我不明白问题出在哪里
这是我的密码:
TextView tvq;
InputStream is;
String[] question = new String[10];
ImageButton[] buttons = new ImageButton[4];
int[] check = {-1, -1 ,-1};
int[] answersid = {R.drawable.israelflag, R.drawable.spainflag, R.drawable.franceflag, R.drawable.greeceflag, R.drawable.egyptflag, R.drawable.unitedstatesflag, R.drawable.brazilflag, R.drawable.japanflag, R.drawable.turkeyflag, R.drawable.iraqflag};
int i, randombutton, correctanswerid ,a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia);
buttons[0] = (ImageButton) findViewById(R.id.im1);
buttons[0].setOnClickListener(this);
buttons[1] = (ImageButton) findViewById(R.id.im2);
buttons[1].setOnClickListener(this);
buttons[2] = (ImageButton) findViewById(R.id.im3);
buttons[2].setOnClickListener(this);
buttons[3] = (ImageButton) findViewById(R.id.im4);
buttons[3].setOnClickListener(this);
tvq = (TextView) findViewById(R.id.tvq);
try {
setQuestion();
} catch (IOException e) {
e.printStackTrace();
}
game();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_trivia, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setQuestion() throws IOException {
int z = 0;
String st = "";
is = this.getResources().openRawResource(R.raw.questions);
InputStreamReader isr1 = new InputStreamReader(is);
BufferedReader br1 = new BufferedReader(isr1);
while ((st = br1.readLine()) != null) {
question[z] = st;
z++;
}
is.close();
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.im1) {
if (R.id.im1 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im2) {
if (R.id.im2 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im3) {
if (R.id.im3 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
if (id == R.id.im4) {
if ( R.id.im4 == correctanswerid)
Toast.makeText(this, "correct", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "incorrect", Toast.LENGTH_LONG).show();
game();
}
}
public void game()
{
i = (int) (8 * Math.random());
tvq.setText(question[i]);
randombutton = (int) (3 * Math.random());
buttons[randombutton].setImageResource(answersid[i]);
correctanswerid = buttons[randombutton].getId();
ImageButton temp = buttons[randombutton];
buttons[randombutton] = buttons[buttons.length-1];
for (int s = 0; s < buttons.length - 1; s++) {
a = (int) (9 * Math.random());
for (int k = 0; k < check.length ; k++) {
if (check[k] == a || a == i ) {
a = (int) (9 * Math.random());
while (check[k] == i || a == 1)
a = (int) (9 * Math.random());
}
}
check[s] = a;
buttons[s].setImageResource(answersid[a]);
}
}
}
谢谢你的帮助!(很抱歉我的英语不好)问题在于问题数组
String[] question = new String[10];
这里的z值超过了9,即问题数组的最大索引
while ((st = br1.readLine()) != null) {
question[z] = st;
z++;
}
你需要阻止它超过9点。像这样:
while (((st = br1.readLine()) != null) && (z < 10)) {
while(((st=br1.readLine())!=null)和&(z<10)){
问题在于问题数组
String[] question = new String[10];
这里的z值超过了9,即问题数组的最大索引
while ((st = br1.readLine()) != null) {
question[z] = st;
z++;
}
你需要阻止它超过9。像这样:
while (((st = br1.readLine()) != null) && (z < 10)) {
while(((st=br1.readLine())!=null)和&(z<10)){
谢谢老兄!真的很有用!谢谢老兄!真的很有用!