setText 使应用程序意外停止

发布于 2024-12-11 11:50:36 字数 4630 浏览 4 评论 0原文

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

After added checkAnsText.setText the application stopped unexpectedly. I cant find where the error is. I hope someone can help me

Here is the code

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

}
}

Here is the error

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-12-18 11:50:36

您应该在方法 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;
    }
}

You should declare your TextView inside the method checkAnswer() : your method should be like this :

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;
    }
}
凉城 2024-12-18 11:50:36

您正尝试从初始值设定项或构造函数调用 findViewById()。不支持此操作。在调用 setContentView()之后,您才能调用 findViewById()

You are attempting to call findViewById() from an initializer or a constructor. This is not supported. You cannot call findViewById() until after your call to setContentView().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文