2014-10-16 69 views
-1

我想在end_page.xml的textview中打印用户分数。我可以在activity_main.xml中做到这一点,但是当我改变布局时,我看到了这个错误。我想我看到这个错误,因为scoreEndBox.setText(scoreNum + "");。我该如何解决它?setText()nullPointerException错误

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

可能重复什么是空指针异常,以及如何我能解决它吗?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ben75 2014-10-16 21:08:27

+0

你能告诉我哪个是MainActivity的第102行? – 2014-10-16 21:13:54

回答

0

这就是我conjuring-(我爱jux这个词。)笑-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,所以你必须做的是再次实例化并设置文本,并且不会有任何空指针异常

这样做

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是在布局..让我知道,如果有帮助的[

+0

是的,它改变了第二个scoreEndBox变量的名字。代码如下:else {0} {0} setContentView(R.layout.end_page); TextView scoreEndBox2 =(TextView)findViewById(R.id.scoreEnd); scoreEndBox2.setText(scoreNum +“”); scoreNum = 0; }感谢您的帮助! – hco 2014-10-17 21:41:48

+0

太棒了!即时通讯很高兴它帮助...顺便说一句,如果它帮助你,然后喜欢或接受它作为答案..快乐codding @ user200306 – Elltz 2014-10-17 21:49:04