日志:
D/ViewRootImpl@d8cc6ae[GlavniyActivity]: ViewPostImeInputStage processPointer 0
D/ViewRootImpl@d8cc6ae[GlavniyActivity]: ViewPostImeInputStage processPointer 1
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
D/TextView: setTypeface with style : 0
D/ViewRootImpl@40adc3c[GlavniyActivity]: ThreadedRenderer.create() translucent=true
D/InputTransport: Input channel constructed: fd=81
D/ViewRootImpl@40adc3c[GlavniyActivity]: setView = DecorView@a223dc5[GlavniyActivity] touchMode=true
D/ViewRootImpl@40adc3c[GlavniyActivity]: dispatchAttachedToWindow
D/ViewRootImpl@40adc3c[GlavniyActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[37,1074][1403,1570] result=0x27 surface={isValid=true 480721428480} surfaceGenerationChanged=true
mHardwareRenderer.initialize() mSurface={isValid=true 480721428480} hwInitialized=true
D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000, [1590x720]-format:1
D/ScrollView: onsize change changed
D/ViewRootImpl@40adc3c[GlavniyActivity]: MSG_WINDOW_FOCUS_CHANGED 1
mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 480721428480}
D/ViewRootImpl@40adc3c[GlavniyActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl@d8cc6ae[GlavniyActivity]: MSG_WINDOW_FOCUS_CHANGED 0
问题是,当我选择正确的答案时,程序突然停止,我不明白为什么。
我的活动:
public class GlavniyActivity extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score, question;
private Questions mQuestions = new Questions();
private String mAnswer;
private int mScore = 0;
private int mQuestionsLenght = mQuestions.mQuestions.length;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glavniy);
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
r = new Random();
question = (TextView) findViewById(R.id.question);
score = (TextView) findViewById(R.id.score);
score.setText("Score: " + mScore);
updataQuestion(r.nextInt(mQuestionsLenght));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer1.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updataQuestion(r.nextInt(mQuestionsLenght));
}else {gameOver();}
}
});
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer2.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updataQuestion(r.nextInt(mQuestionsLenght));
}else {gameOver();}
}
});
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer3.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updataQuestion(r.nextInt(mQuestionsLenght));
}else {gameOver();}
}
});
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answer4.getText() == mAnswer) {
mScore++;
score.setText("Score: " + mScore);
updataQuestion(r.nextInt(mQuestionsLenght));
}else {gameOver();}
}
});
}
private void updataQuestion(int num) {
question.setText(mQuestions.getQuestion(num));
answer1.setText(mQuestions.getChoice1(num));
answer2.setText(mQuestions.getChoice2(num));
answer3.setText(mQuestions.getChoice3(num));
answer4.setText(mQuestions.getChoice4(num));
mAnswer = mQuestions.getCorrectAnswer(num);
}
private void gameOver(){
AlertDialog.Builder alertDialogBuilder=new AlertDialog.Builder(GlavniyActivity.this);
alertDialogBuilder
.setMessage("Игра окончена. Выбран неправильный ответ!" + mScore+ " points.")
.setCancelable(false)
.setPositiveButton("Новая Игра!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(),GlavniyActivity.class));
finish();
}
})
.setNegativeButton("ВЫХОД!",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
alertDialog.show();
}
}