setText使应用程序意外停止

时间:2011-10-22 14:38:30

标签: android textview settext

添加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)

2 个答案:

答案 0 :(得分:1)

您应该在方法checkAnswer()中声明TextView:您的方法应该是这样的:

private boolean checkAnswer() {
    //here where you should declare your textView
    TextView checkAnsText = (TextView) findViewById(R.id.checkAns);

    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;
    }
}

答案 1 :(得分:0)

您正尝试从初始值设定项或构造函数调用findViewById()。这不受支持。在致电findViewById() 后,您无法在之前致电setContentView()