【发布时间】:2020-02-03 15:31:52
【问题描述】:
我正在创建一个包含游戏的培训应用程序。其中一个游戏是真假测验,当屏幕上出现一个陈述时,用户点击真假。
当我回答 true 或 false 时,一些语句有效,但在几个语句之后,应用程序因 Index out of bounds 错误而终止。
我有一个游戏类,还有一个陈述类和相应的答案。
当我运行应用程序时,在几个问题后应用程序关闭并且我收到这些错误。
Process: com.example.securityapp, PID: 28776
java.lang.IndexOutOfBoundsException: Index: 8, Size: 8
at java.util.ArrayList.get(ArrayList.java:437)
at
com.example.securityapp.industry_standards_questions.checkAnswer(industry_standards_questions.java:104)
at com.example.securityapp.industry_standards_questions.access$000(industry_standards_questions.java:20)
at com.example.securityapp.industry_standards_questions$2.onClick(industry_standards_questions.java:71)
package com.example.securityapp;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;
import android.media.Image;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.Collections;
public class industry_standards_questions extends AppCompatActivity {
TextView stmnt;
ImageView image_true, image_false;
Button points;
Statements tfStatements;
int statementsLength;
ArrayList<Item> statementList;
int currentStatement = 0;
int grade = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_industry_standards_questions);
stmnt = (TextView) findViewById(R.id.statement_TV);
image_true = (ImageView) findViewById(R.id.trueImage);
image_false = (ImageView) findViewById(R.id.falseImage);
tfStatements = new Statements();
statementsLength = tfStatements.tfStatements.length;
statementList = new ArrayList<>();
points = (Button) findViewById(R.id.final_score);
for(int i = 0; i < statementsLength; i++){
statementList.add(new Item(tfStatements.getStatment(i), tfStatements.getAnswer(i)));
}
Collections.shuffle(statementList);
setStmnt(currentStatement);
points.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
score();
}
});
image_true.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentStatement++;
if(checkAnswer(currentStatement)){
grade++;
if(currentStatement < statementsLength){
setStmnt(currentStatement);
}
}
}
});
image_false.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentStatement++;
if(!checkAnswer(currentStatement)){
grade++;
if(currentStatement < statementsLength){
setStmnt(currentStatement);
}
}
}
});
}
// show Statement
private void setStmnt(int number){
stmnt.setText(statementList.get(number).getStatement());
}
//check is answer is right
private boolean checkAnswer(int number){
String answer = statementList.get(number).getAnswer();
return answer.equals("true");
}
public void score(){
Toast.makeText(this, "You scored " + grade + " points!", Toast.LENGTH_LONG).show();
grade = 0;
}
}
package com.example.securityapp;
public class Statements {
public String tfStatements [] = {
"10% of U.S. companies use the Cybersecurity Framework",
"Prevent is an element within the framework",
"Respond is an element within the framework",
"Access control is a future priority",
"Risk Assessment is a current priority",
"Asset management is a current priority",
"The framework can be customised",
"By 2020, an estimated 70% of U.S. companies will use the framework"
};
public String tfAnswers [] = {
"false",
"false",
"true",
"false",
"true",
"true",
"true",
"false"
};
public String getStatment (int number) {
return tfStatements[number];
}
public String getAnswer (int number) {
return tfAnswers[number];
}
}
这是语句类
package com.example.securityapp;
public class Statements {
public String tfStatements [] = {
"10% of U.S. companies use the Cybersecurity Framework",
"Prevent is an element within the framework",
"Respond is an element within the framework",
"Access control is a future priority",
"Risk Assessment is a current priority",
"Asset management is a current priority",
"The framework can be customised",
"By 2020, an estimated 70% of U.S. companies will use the framework"
};
public String tfAnswers [] = {
"false",
"false",
"true",
"false",
"true",
"true",
"true",
"false"
};
public String getStatment (int number) {
return tfStatements[number];
}
public String getAnswer (int number) {
return tfAnswers[number];
}
}
【问题讨论】:
-
你有 8 条语句...
-
@LucaMurra 当然是。他有 8 个编号为 0-7 的语句,他正在尝试访问不存在的 #8。
标签: java android indexoutofboundsexception