I want to print user score in the textview in end_page.xml
. I can do it in activity_main.xml
but when i change the layout i see this error. I think I am seeing this error because of scoreEndBox.setText(scoreNum + "");
. How can I solve it?
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;
}
}
}
Error:
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)