gpt4 book ai didi

android - setText 使应用程序意外停止

转载 作者:行者123 更新时间:2023-11-30 04:30:13 24 4
gpt4 key购买 nike

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

最佳答案

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

关于android - setText 使应用程序意外停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7860295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com