Reputation: 43
I'm doing a Q&A app, and I want to increment a int everytime people mark the right answer. As I'm working with multiple acitivies (one activity for each question) I'm passing an Extra to every Activity, and the game is closing when I answer the 1º question, and I wanna know how can I solve this problem. The code is this below: `
@Override
public class QuestionOneActivity extends AppCompatActivity {
int score = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_one);
Button btn = (Button)findViewById(R.id.button);
final RadioButton rd1 = (RadioButton)findViewById(R.id.radioButton);
final RadioButton rd2 = (RadioButton)findViewById(R.id.radioButton2);
final RadioButton rd3 = (RadioButton)findViewById(R.id.radioButton3);
final RadioButton rd4 = (RadioButton)findViewById(R.id.radioButton4);
final RadioButton rd5 = (RadioButton)findViewById(R.id.radioButton5);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd2.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd3.isChecked()){
++score;
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd4.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd5.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
}
});
}
}
And the Question Two Activity:
@Override
public class QuestionTwoActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_two);
Intent intent = getIntent();
int score = intent.getIntExtra("score", 0);
Button btn2 = (Button)findViewById(R.id.button2);
final RadioButton rd6 = (RadioButton)findViewById(R.id.radioButton6);
final RadioButton rd7 = (RadioButton)findViewById(R.id.radioButton7);
final RadioButton rd8 = (RadioButton)findViewById(R.id.radioButton8);
final RadioButton rd9 = (RadioButton)findViewById(R.id.radioButton9);
final RadioButton rd10 = (RadioButton)findViewById(R.id.radioButton10);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd6.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd7.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd8.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd9.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd10.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
}
});
}
}
`The problem is: It shows a error message on the second question activity, on the score variable. It says that I have to use the Final declaration, but I can't use final because I have to increment score if person answers correctly the question. How can I solve this?
Upvotes: 1
Views: 102
Reputation: 20616
I've answered a similar question How to save score to SharedPreferences then update it.
This is how to do it :
This is how you create the SharedPreferences
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("score", YOUR_INT_VALUE);
editor.commit();
And then where you need to get that value, you simply do that :
sharedPreferences.getInt("score", 0)
Hope it's clear for you.
Your QuestionOneActivity
should look like :
public class QuestionOneActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int score = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_one);
//If you want you can initialite the score to 0 doing this (IT'S NOT NECESSARY)
SharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
editor.putInt("score", 0);
editor.commit();
Button btn = (Button)findViewById(R.id.button);
final RadioButton rd1 = (RadioButton)findViewById(R.id.radioButton);
final RadioButton rd2 = (RadioButton)findViewById(R.id.radioButton2);
final RadioButton rd3 = (RadioButton)findViewById(R.id.radioButton3);
final RadioButton rd4 = (RadioButton)findViewById(R.id.radioButton4);
final RadioButton rd5 = (RadioButton)findViewById(R.id.radioButton5);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd2.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
//Everytime you want to ++ your score, now you'll do this
if(rd3.isChecked()){
editor = sharedPreferences.edit();
editor.putInt("score", (sharedPreferences.getInt("score", 0))+1);
editor.commit();
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd4.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd5.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
}
});
}
Then instead of doing
int score = intent.getIntExtra("score", 0);
You can do this :
int score = sharedPreferences.getInt("score", 0);
Note: I'm not sure that this line will work
editor = sharedPreferences.edit();
editor.putInt("score", (sharedPreferences.getInt("score", 0))+1);
editor.commit();
If it does not, just save the SharedPreferences
value to an int
and then do ++
Upvotes: 3
Reputation: 6459
The problem with static classes is that it CAN be garbage collected, under some circumstances. I would advise to use SharedPreferences to handle you scores.
Upvotes: 0
Reputation: 15145
There are a couple of ways you can handle this without using an Intent
, though it is possible while using an Intent
, it is much more cumbersome. I would handle this by using a utility class with public static methods to set and get the score:
public final class ScoreUtil {
private static int score;
public static void setScore(int score) {
this.score = score;
}
public static int getScore() {
return score;
}
public static void addScore(int add) {
score += add;
}
public static void clearScore() {
score = 0;
}
}
This way you can call these methods from any Activity
. Otherwise, your current code should work if you make score
a global variable instead of a local one.
public class Question2Activity extends AppCompatActivity {
private int mScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScore = getIntent().getIntExtra("score", 0);
...
}
}
Upvotes: 1