gpt4 book ai didi

java - 错误不断崩溃 E/AndroidRuntime : FATAL EXCEPTION: main

转载 作者:行者123 更新时间:2023-12-01 17:52:35 28 4
gpt4 key购买 nike

您好,我是一名学生,我的任务量严重重载,而且我的其中一项作品总是崩溃。每当我启动该应用程序时,它就会停止工作。

我是 Android Studio 的新手,我需要更多知识,谢谢!

01-19 22:55:31.562 26778-26778/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ict402.germio.examplequiz, PID: 26778
java.lang.RuntimeException: Unable to start activity ComponentInfo{ict402.germio.examplequiz/ict402.germio.examplequiz.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6361)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at ict402.germio.examplequiz.Question.getChoice4(Question.java:38)
at ict402.germio.examplequiz.MainActivity.updateQuestion(MainActivity.java:120)
at ict402.germio.examplequiz.MainActivity.onCreate(MainActivity.java:49)
at android.app.Activity.performCreate(Activity.java:6675)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:203) 
at android.app.ActivityThread.main(ActivityThread.java:6361) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

这是我在 MainActivity.java 中的代码

    package ict402.germio.examplequiz;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

Button choice1, choice2, choice3, choice4, quit;

TextView score, question;

private Question mQuestions = new Question();

private String mAnswer;
private int mScore = 0;
private int mQuestionsLength = mQuestions.mQuestions.length;
Random r;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

r = new Random();


//Buttons
choice1 = findViewById(R.id.choice1);
choice2 = findViewById(R.id.choice2);
choice3 = findViewById(R.id.choice3);
choice4 = findViewById(R.id.choice4);
quit = findViewById(R.id.quit);

//TextViews
score = findViewById(R.id.score);
question = findViewById(R.id.question);

score.setText("Score: " + mScore);

updateQuestion(r.nextInt(mQuestionsLength));


choice1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (choice1.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}


}
});
choice2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (choice2.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
choice3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (choice3.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
choice4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (choice4.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mQuestionsLength));
} else {
gameOver();
}
}
});
quit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});

}


private void updateQuestion(int num) {

question.setText(mQuestions.getQuestion(num));

choice1.setText(mQuestions.getChoice1(num));
choice2.setText(mQuestions.getChoice2(num));
choice3.setText(mQuestions.getChoice3(num));
choice4.setText(mQuestions.getChoice4(num));


mAnswer = mQuestions.getCorrectAnswer(num);


}


private void gameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder
.setMessage("Game Over! Your Score is " + mScore + "pts.")
.setCancelable(false)
.setPositiveButton("NEW GAME",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

startActivity(new Intent(getApplicationContext(), MainActivity.class));

}
})
.setNegativeButton("EXIT",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

finish();

}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

}
}

这是我的 Question.java Activity

package ict402.germio.examplequiz;

公开课问题{

public String mQuestions [] = {
"Complete the Statement Pogi si _________.",
"What is the our Company Name?",
"What is the Filipino word for Verb?",
"Neil armstrong stepping onto the _________."

};

private String mChoices [][] = {
{"Neil", "Ranier", "Jason"},
{"Google Inc.", "innoTech", "STI"},
{"Panguri", "Pandesal", "Panaguri"},
{"Jupiter", "Moon", "Tae ng Kalabaw"}
};
private String mCorrectAnswers[] = {"Neil", "innoTech", "Pandesal", "Tae ng Kalabaw"};

public String getQuestion(int a) {
String question = mQuestions[a];
return question;
}
public String getChoice1(int a) {
String choice = mChoices[a][0];
return choice;
}
public String getChoice2(int a) {
String choice = mChoices[a][1];
return choice;
}
public String getChoice3(int a) {
String choice= mChoices[a][2];
return choice;
}
public String getChoice4(int a) {
String choice = mChoices[a][3];
return choice;
}

public String getCorrectAnswer(int a) {
String answer = mCorrectAnswers[a];
return answer;
}

}

最佳答案

您遇到 ArrayIndexOutOfBoundsException 问题。这意味着“您正在尝试触摸数组中不存在的元素”

这是因为您有 mChoices[a][3]mChoices 的所有元素只有 3 个元素,这意味着只有索引 [0,1, 2]存在

关于java - 错误不断崩溃 E/AndroidRuntime : FATAL EXCEPTION: main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48343985/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com