错误不断崩溃 E/AndroidRuntime:致命异常:主要 [重复]
Posted
技术标签:
【中文标题】错误不断崩溃 E/AndroidRuntime:致命异常:主要 [重复]【英文标题】:Error Keeps Crashing E/AndroidRuntime: FATAL EXCEPTION: main [duplicate] 【发布时间】:2018-06-28 20:45:31 【问题描述】:您好,我是一名学生,我的任务非常繁重,我的一项工作总是崩溃。每当我启动应用程序时,它都会停止工作。
我是使用 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 ComponentInfoict402.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;
【问题讨论】:
您的选择列表中有 3 个项目,您要求第 4 个项目 【参考方案1】:您有一个ArrayIndexOutOfBoundsException
问题。这意味着“您正在尝试触摸数组中不存在的元素”
这是因为你有mChoices[a][3]
,但mChoices
的所有元素只有3个元素,这意味着只有索引[0,1,2]
存在
【讨论】:
【参考方案2】:在这个选项列表中:
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
【讨论】:
以上是关于错误不断崩溃 E/AndroidRuntime:致命异常:主要 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2