我正在做一个Q& A应用程序,我想在每次人们标记正确答案时增加一个int。当我正在处理多个活动(每个问题一个活动)时,我正在向每个活动传递一个Extra,当我回答1º问题时游戏正在关闭,我想知道如何解决这个问题。代码如下:`
@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);
}
}
});
}
}
问题二活动:
@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);
}
}
});
}
}
`问题是:它在分数变量上的第二个问题活动上显示错误消息。它说我必须使用最终声明,但我不能使用final,因为如果人正确回答问题,我必须增加分数。我该如何解决这个问题?
答案 0 :(得分:3)
我已经回答了类似的问题How to save score to SharedPreferences then update it。
这是怎么做的:
这是您创建SharedPreferences
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("score", YOUR_INT_VALUE);
editor.commit();
然后你需要获得这个价值,你只需这样做:
sharedPreferences.getInt("score", 0)
希望你能清楚。
您的QuestionOneActivity
应如下:
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);
}
}
});
}
然后而不是做
int score = intent.getIntExtra("score", 0);
你可以这样做:
int score = sharedPreferences.getInt("score", 0);
注意:我不确定此行是否有效
editor = sharedPreferences.edit();
editor.putInt("score", (sharedPreferences.getInt("score", 0))+1);
editor.commit();
如果没有,只需将SharedPreferences
值保存到int
,然后再执行++
答案 1 :(得分:1)
有两种方法可以在不使用Intent
的情况下处理此问题,但在使用Intent
时可能会更加繁琐。我会通过使用带有公共静态方法的实用程序类来设置和获取分数来处理这个问题:
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;
}
}
这样您就可以从任何Activity
调用这些方法。否则,如果您将score
设置为全局变量而不是本地变量,则当前代码应该有效。
public class Question2Activity extends AppCompatActivity {
private int mScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScore = getIntent().getIntExtra("score", 0);
...
}
}
答案 2 :(得分:0)
静态类的问题在于,在某些情况下可以进行垃圾回收。我建议使用SharedPreferences来处理你的分数。