我有这个" Android应用程序意外停止,请再试一次" (每次我运行游戏类),结果发现NullPointerException存在问题,但我不太确定如何解决它......
java代码
public class play extends Activity implements OnClickListener {
private Question currentQuestion;
private int currentQuestionIndex;
private ArrayList<Button> questionButton;
private TextView questionstextview;
private TextView questionnumber;
private TextView playerfeedback;
public TextView displayscore;
public int score;
private List<Question> QuestionList;
private int answerchoice;
public static int totalanswer;
public static int correctanswer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
Log.i("playclass", "this is play class running");
questionButton = new ArrayList<Button>();
questionButton.add((Button) findViewById (R.id.answerbutton1));
questionButton.add((Button) findViewById (R.id.answerbutton2));
questionButton.add((Button) findViewById (R.id.answerbutton3));
questionButton.add((Button) findViewById (R.id.answerbutton4));
currentQuestion = null;
currentQuestionIndex = 0;
View AnswerButton1 = findViewById(R.id.answerbutton1);
AnswerButton1.setOnClickListener(this);
View AnswerButton2 = findViewById(R.id.answerbutton2);
AnswerButton2.setOnClickListener(this);
View AnswerButton3 = findViewById(R.id.answerbutton3);
AnswerButton3.setOnClickListener(this);
View AnswerButton4 = findViewById(R.id.answerbutton4);
AnswerButton4.setOnClickListener(this);
Log.i("playclass", "aftersetlistener");
QuestionList = new ArrayList<Question>();
ArrayList <String> answer = new ArrayList<String>();
answer.add("8");
answer.add("9");
answer.add("3");
answer.add("1");
QuestionList.add(new Question("what is 4+4", answer, 0));
answer.add("17");
answer.add("20");
answer.add("15");
answer.add("14");
QuestionList.add(new Question("what is 7+8?", answer, 3));
answer.add("20");
answer.add("30");
answer.add("19");
answer.add("34");
QuestionList.add(new Question("what is 10+10?", answer, 0));
answer.add("12");
answer.add("11");
answer.add("13");
answer.add("14");
QuestionList.add(new Question("what is 6+6?", answer, 0));
answer.add("6");
answer.add("5");
answer.add("4");
answer.add("7");
QuestionList.add(new Question("what is 4+3?", answer, 3));
answer.add("7");
answer.add("9");
answer.add("10");
answer.add("11");
QuestionList.add(new Question("what is 3+7?", answer, 2));
questionstextview = (TextView) findViewById (R.id.questionstextview);
questionnumber = (TextView) findViewById (R.id.questionnumber);
displayscore = (TextView) findViewById (R.id.displayscore);
StartTrivia();
}
private void ButtonPress (Button answerButton){
final MediaPlayer soundfx = MediaPlayer.create(getApplicationContext(), R.raw.click);
soundfx.start();
for (int i=0; i< questionButton.size(); i++)
if (questionButton.get(i) ==answerButton)
if (i==currentQuestion.getAnswerIndex()){
score=+5;
totalanswer++;
correctanswer++;
displayscore.setText(Integer.toString(score));
Toast.makeText(getApplicationContext(), "Correct!!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "Incorrect!!", Toast.LENGTH_SHORT).show();
}
currentQuestionIndex++;
if (currentQuestionIndex < QuestionList.size()){
StartTrivia();
}
else{
Intent result = new Intent (this, finalscreen.class);
startActivity(result);
}
}
public void StartTrivia(){
Log.i("playclass", "running StartTrivia()");
currentQuestion = QuestionList.get(currentQuestionIndex);
Log.i("playclass", "after get current question");
questionstextview.setText(currentQuestion.getquestion());
Log.i("playclass", "after set current question");
questionnumber.setText(Integer.toString(currentQuestionIndex+1));
Log.i("playclass", "after convert int to string for question number");
for (int i = 0; i < questionButton.size(); i++)
{
questionButton.get(i).setText(currentQuestion.getanswer().get(i));
Log.i("playclass", "after get question button");
}
}
public play() {
}
@Override
public void onClick(View v) {
}
}
LogCat
I/mainactivity(372): this is Main Activity running<br>
I/playclass(372): this is play class running<br>
I/playclass(372): aftersetlistener<br>
I/playclass(372): running StartTrivia()<br>
I/playclass(372): after get current question<br>
I/playclass(372): after set current question<br>
I/playclass(372): after convert int to string for question number<br>
D/AndroidRuntime(372): Shutting down VM<br><br><br>
W/dalvikvm(372): threadid=1: thread exiting with uncaught exception (group=0x40015560)<br>
E/AndroidRuntime(372): FATAL EXCEPTION: main<br>
E/AndroidRuntime(372): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.quizgame/com.example.quizgame.play}: java.lang.NullPointerException
E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime(372): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(372): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(372): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(372): at android.os.Handler.dispatchMessage(Handler.java:99)<br>
E/AndroidRuntime(372): at android.os.Looper.loop(Looper.java:123)<br>
E/AndroidRuntime(372): at android.app.ActivityThread.main(ActivityThread.java:3683)<br>
E/AndroidRuntime(372): at java.lang.reflect.Method.invokeNative(Native Method)<br>
E/AndroidRuntime(372): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)<br><br>
E/AndroidRuntime(372): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)<br>
E/AndroidRuntime(372): at dalvik.system.NativeStart.main(Native Method)<br>
E/AndroidRuntime(372): Caused by: java.lang.NullPointerException<br>
E/AndroidRuntime(372): at com.example.quizgame.play.StartTrivia(play.java:157)<br>
E/AndroidRuntime(372): at com.example.quizgame.play.onCreate(play.java:98)<br>
E/AndroidRuntime(372): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)<br>
E/AndroidRuntime(372): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)<br>
E/AndroidRuntime(372): ... 11 more
答案 0 :(得分:0)
看起来你有一个索引问题 - 这行是否真的如此:
questionButton.get(i).setText(currentQuestion.getanswer().get(i));
应该有&#34; questionButton&#34;的索引与&#34; getanswer()&#34;的索引相同对于当前的问题?答案数组的大小是多少?它可能是getanswer()没有像questionButton那样多的元素
答案 1 :(得分:0)
在onCreate方法的第11行中,您将currentQuestion对象初始化为null,而您从未更改过它