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)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在方法
checkAnswer()
中声明您的 TextView :您的方法应该如下所示:You should declare your TextView inside the method
checkAnswer()
: your method should be like this :您正尝试从初始值设定项或构造函数调用
findViewById()
。不支持此操作。在调用setContentView()
之后,您才能调用findViewById()
。You are attempting to call
findViewById()
from an initializer or a constructor. This is not supported. You cannot callfindViewById()
until after your call tosetContentView()
.