我的android应用程序开始崩溃偶尔当我按4个按钮之一后,我实现了代码生成另一个方程



我正在制作一个应用程序,它有一个计时器,从30秒开始倒计时。你得到一个方程,有4个按钮,其中1个是正确的。游戏会记录分数,每次玩家按下按钮都会生成一个新的方程式。我还没有完成代码,但一般的概念是可行的。计时器结束后,我没有结束等。我只是想知道是否有人可以看看我的代码,看看为什么一半的时间它工作和另一半崩溃。不要评判我糟糕的编码概念。它只崩溃有时当我按下按钮后,我实现了代码刷新生成的方程和按钮上的随机数



import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public int sum;
public Button button1;
public Button button2;
public Button button3;
public Button button4;
public Button[] answerButtons;
public int randButton;
public int counter;
public int amountCorrect;
public TextView scoreTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.answer1);
button2 = findViewById(R.id.answer2);
button3 = findViewById(R.id.answer3);
button4 = findViewById(R.id.answer4);
answerButtons = new Button[]{button1, button2, button3, button4};
scoreTextView = findViewById(R.id.scoreTextView);
counter = 0;
amountCorrect = 0;
runEquation();//runs all the methods to create an equation and the numbers stored in the buttons
timerCountDown();
}//onCreate()
public void runEquation(){
generateEquation();
getButtonNumbers();
setRandomButtonAnswer();
}//runEquation()
public void timerCountDown(){
TextView timerTextView = findViewById(R.id.timerTextView);
int lengthOfTime = 30; //amount of time for the timer in seconds
CountDownTimer timer = new CountDownTimer(lengthOfTime*1000, 1000) {//*1000 because it is measure in milliseconds
@Override
public void onTick(long timeLeft) {
timerTextView.setText(Long.toString(timeLeft/1000) + 's');//'s' represents seconds
}
@Override
public void onFinish() {
}
}.start();
}//timerCountDown()
public void generateEquation(){//generates 2 random numbers between 3 and 15 and adds them together in variable sum
int numOne = randomNumber(3, 15); //3 is min number, 15 is max number
int numTwo = randomNumber(3, 15); //3 is min number, 15 is max number
sum = numOne + numTwo;//public int to check with the equations generated by the buttons
displayEquation(numOne, numTwo, sum);
}//generateEquation()
public int randomNumber(int min, int max){//generates a random number between min and max parameters
int randNum = (int) (Math.random() * (max-min) + min);//generates a random number between between the min and max parameters
return randNum;
}//randomNumber
public void displayEquation(int numOne, int numTwo, int sum){
TextView equationTextView = findViewById(R.id.equationTextView);
equationTextView.setText(numOne + " + " + numTwo + " = ?");
}//displayEquation()

public void getButtonNumbers(){//sets the text of each of the buttons to a number generated from generateButtonNumber()
for(int i = 0; i < 4; i++){
answerButtons[i].setText(Integer.toString(generateButtonNumber(i)));
}//end of for
}//getButtonNumbers()
public int generateButtonNumber(int index){//generates a random number between 3 and 15
int min = 3;
int max = 15;
int randNum = (int) (Math.random() * (max - min) + min);
return randNum;
}//generateButtonNumber()
public void setRandomButtonAnswer(){//randomly overwrites one of the 4 buttons as the correct answer
int min = 1;
int max = 5;//not inclusive (1-4)
randButton = (int) (Math.random() * (max - min) + min);
answerButtons[randButton].setText(Integer.toString(sum));
}//setRandomButtonAnswer
public void checkAnswer(View v){
Button answerButton = (Button) findViewById(v.getId());
int answer = Integer.parseInt(answerButton.getText().toString());
if(answer == sum){
correctAnswer();
}
else{
wrongAnswer();
}
}//checkAnswer()
public void correctAnswer(){
counter++;
amountCorrect++;
scoreTextView.setText(Integer.toString(amountCorrect) + " / " + Integer.toString(counter));
runEquation();
}//correctAnswer()
public void wrongAnswer(){
counter++;
scoreTextView.setText(Integer.toString(amountCorrect) + " / " + Integer.toString(counter));
runEquation();
}//wrongAnswer()
}```

问题的根源在于以下方法:

public void setRandomButtonAnswer(){//randomly overwrites one of the 4 buttons as the correct answer
int min = 1;
int max = 5;//not inclusive (1-4)
randButton = (int) (Math.random() * (max - min) + min);
answerButtons[randButton].setText(Integer.toString(sum));
}//setRandomButtonAnswer

你得到了一个IndexOutOfBoundsException:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.example.myapplication.MainActivity.setRandomButtonAnswer(MainActivity.java:104)
at com.example.myapplication.MainActivity.runEquation(MainActivity.java:45)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:38)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)

应该改成:

int max = 4;

所以,新方法应该是:

public void setRandomButtonAnswer(){//randomly overwrites one of the 4 buttons as the correct answer
int min = 1;
int max = 4;//not inclusive (1-4)
randButton = (int) (Math.random() * (max - min) + min);
answerButtons[randButton].setText(Integer.toString(sum));
}//setRandomButtonAnswer

希望这能修复它。

最新更新