你好,我是一名学生,我严重超负荷工作任务,我的一件作品总是崩溃。每当我启动应用程序时,它都会一直停止工作。
Im Newbie使用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活动
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;
}
}
答案 0 :(得分:3)
您有ArrayIndexOutOfBoundsException
个问题。这意味着“你试图触摸数组中不存在的元素”
这是因为您有mChoices[a][3]
,但mChoices
的所有元素只有3个元素,这意味着只存在索引[0,1,2]
答案 1 :(得分:1)
在此选项列表中:
private String mChoices [][] = {
{"Neil", "Ranier", "Jason"},
{"Google Inc.", "innoTech", "STI"},
{"Panguri", "Pandesal", "Panaguri"},
{"Jupiter", "Moon", "Tae ng Kalabaw"}
你有4x3项目,方法是:
public String getChoice4(int a) {
String choice = mChoices[a][3];
return choice;
}
你试图获得第4项(3个元素是第4个,因为第0个索引是第一个)
您可以在此处调用此方法:
choice4.setText(mQuestions.getChoice4(num));
在private void updateQuestion(int num)
因为您正试图获取阵列中不存在的项目,所以您可以获得ArrayIndexOutOfBoundsException