После добавления checkAnsText.setText приложение неожиданно остановилось. Я не могу найти, где ошибка. Я надеюсь, что кто-то может мне помочь
Вот код
public class QuestActivity extends Activity implements OnClickListener{
private Quest currentQ;
private SetGame currentGame;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questactivitylayout);
currentGame = ((TheApplication)getApplication()).getCurrentGame();
currentQ = currentGame.getNextQuestion();
Button nextBtn = (Button) findViewById(R.id.nextBtn);
Button confirmBtn = (Button) findViewById(R.id.confirmBtn);
confirmBtn.setOnClickListener(confirmBtn_Listener);
setQuestions();
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/**
* check if end of game
*/
if (currentGame.isGameOver()){
Intent i = new Intent(QuestActivity.this, TheEndActivity.class);
startActivity(i);
finish();
}
else{
Intent i = new Intent(QuestActivity.this, QuestActivity.class);
startActivity(i);
finish();
}
}
});
}
private void setQuestions() {
//set the question text from current question
String question = ConvAddition.capitalise(currentQ.getQuestion()) + "?";
TextView qText = (TextView) findViewById(R.id.question);
qText.setText(question);
//set the available options
List<String> answers = currentQ.getQuestionOptions();
TextView option1 = (TextView) findViewById(R.id.answer1);
option1.setText(ConvAddition.capitalise(answers.get(0)));
TextView option2 = (TextView) findViewById(R.id.answer2);
option2.setText(ConvAddition.capitalise(answers.get(1)));
TextView option3 = (TextView) findViewById(R.id.answer3);
option3.setText(ConvAddition.capitalise(answers.get(2)));
TextView option4 = (TextView) findViewById(R.id.answer4);
option4.setText(ConvAddition.capitalise(answers.get(3)));
}
private View.OnClickListener confirmBtn_Listener= new View.OnClickListener() {
@Override
public void onClick(View arg0) {
/**
* validate a checkbox has been selected
*/
if (!checkAnswer()) return;
}
};
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch (keyCode)
{
case KeyEvent.KEYCODE_BACK :
return true;
}
return super.onKeyDown(keyCode, event);
}
TextView checkAnsText = (TextView) findViewById(R.id.checkAns);
private boolean checkAnswer() {
String answer = getSelectedAnswer();
if (answer==null){
return false;
}
else {
if (currentQ.getAnswer().equalsIgnoreCase(answer))
{
currentGame.incrementRightAnswers();
checkAnsText.setText ("Correct Answer " + answer);
Log.e("Correct Answer", answer, null);
}
else{
currentGame.incrementWrongAnswers();
checkAnsText.setText ("Wrong Answer " + answer);
Log.e("Wrong Answer", answer, null);
}
return true;
}
}
private String getSelectedAnswer() {
RadioButton c1 = (RadioButton)findViewById(R.id.answer1);
RadioButton c2 = (RadioButton)findViewById(R.id.answer2);
RadioButton c3 = (RadioButton)findViewById(R.id.answer3);
RadioButton c4 = (RadioButton)findViewById(R.id.answer4);
if (c1.isChecked())
{
return c1.getText().toString();
}
if (c2.isChecked())
{
return c2.getText().toString();
}
if (c3.isChecked())
{
return c3.getText().toString();
}
if (c4.isChecked())
{
return c4.getText().toString();
}
return null;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
Вот ошибка
Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(277): at android.app.Activity.findViewById(Activity.java:1637)
ERROR/AndroidRuntime(277): at com.rika.QuestActivity.<init>(QuestActivity.java:113)
ERROR/AndroidRuntime(277): at java.lang.Class.newInstanceImpl(Native Method)
ERROR/AndroidRuntime(277): at java.lang.Class.newInstance(Class.java:1429)
ERROR/AndroidRuntime(277): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
ERROR/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)