我想在end_page.xml
的textview中打印用户分数。我可以在activity_main.xml
中执行此操作,但是当我更改布局时,我会看到此错误。我想因为scoreEndBox.setText(scoreNum + "");
我看到了这个错误。我该如何解决?
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page);
}
public Question question;
public GameTimer gameTimer;
public int scoreNum = 0;
public void StartGame(View v){
setContentView(R.layout.activity_main);
ShowQuestion();
}
public void ShowQuestion(){
question = new Question();
int[] choices = new int[4];
TextView questionBox = (TextView)findViewById(R.id.questionBox);
Button aChoice = (Button)findViewById(R.id.a);
Button bChoice = (Button)findViewById(R.id.b);
Button cChoice = (Button)findViewById(R.id.c);
Button dChoice = (Button)findViewById(R.id.d);
Random r = new Random();
int var = r.nextInt(3);
question.Questioner();
String showedQuestion = question.question;
choices[0] = question.selection1;
choices[1] = question.selection2;
choices[2] = question.selection3;
choices[3] = question.selection4;
choices[var] = question.answer;
questionBox.setText(showedQuestion);
aChoice.setText(choices[0] + "");
bChoice.setText(choices[1] + "");
cChoice.setText(choices[2] + "");
dChoice.setText(choices[3] + "");
}
public void Score(){
TextView score = (TextView)findViewById(R.id.score);
scoreNum = scoreNum + 55;
score.setText(scoreNum + "");
}
public void AnswerChecker(View v){
Button userChoice = (Button)findViewById(v.getId());
String userAnswer = userChoice.getText().toString();
//TextView questionBox = (TextView)findViewById(R.id.questionBox);
TextView scoreEndBox = (TextView)findViewById(R.id.scoreEnd);
int userAnswerInt = Integer.parseInt(userAnswer);
int rightAnswerInt = question.answer;
if (userAnswerInt == rightAnswerInt){
ShowQuestion();
Score();
} else {
//questionBox.setText(R.string.wrong_answer);
//scoreEndBox.setText(scoreNum + "");
setContentView(R.layout.end_page);
scoreEndBox.setText(scoreNum + "");
scoreNum = 0;
}
}
}
错误:
10-16 20:39:58.070 2640-2640/com.ebook.hco.mindcalc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ebook.hco.mindcalc, PID: 2640
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.ebook.hco.mindcalc.MainActivity.AnswerChecker(MainActivity.java:102)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这就是我所说的 - (我jux喜欢这个词。)lol -im因为这个而想到它
else {
//questionBox.setText(R.string.wrong_answer);
//scoreEndBox.setText(scoreNum + "");
setContentView(R.layout.end_page); //this is the reason
scoreEndBox.setText(scoreNum + "");
scoreNum = 0;
}
setcontentview从我所知道的重置everthing所以你要做的是再次实例化它并设置文本,不会有任何nullpointer异常
这样做
else {
//questionBox.setText(R.string.wrong_answer);
//scoreEndBox.setText(scoreNum + "");
setContentView(R.layout.end_page);
TextView scoreEndBox = (TextView)findViewById(R.id.scoreEnd);
scoreEndBox.setText(scoreNum + "");
scoreNum = 0;
}
并确保R.id.scoreEnd在该布局中..让我知道它是否有帮助