【发布时间】:2017-05-29 01:17:08
【问题描述】:
我试图在我的应用程序中实现一个高分函数,并且我创建了一个分数的 int 变量,但是当我尝试在另一个类中调用它时,我不断收到“无法解析符号'score'”。
int score = getIntent().getIntExtra("score", 0);
scoreLabel.setText(score + "");
SharedPreferences settings = getSharedPreferences("Game_Data", Context.MODE_PRIVATE);
int highScore = settings.getInt("High_Score", 0);
if(SystemClock.elapsedRealtime() > highScore){
highScoreLabel.setText("High Score : " + score);
//save
SharedPreferences.Editor editor = settings.edit();
editor.putInt("High_Score", score);
editor.commit();
}else {
highScoreLabel.setText("High Score : " + highScore);
}
}
然后我在调用函数的类中有以下代码,但变量“分数”一直显示为无法解析符号。我能做什么?
Intent intent = new Intent(getApplicationContext(), scoreKeeper.class);
intent.putExtra("score", score);
startActivity(intent);
请记住,我是一个初学者,所以我可能会错过一些简单的事情。 谢谢。
【问题讨论】:
-
确保在您的班级中将分数设置为全局变量
-
如果两个代码都在不同的方法上,则将变量分数保留为类级别(全局)变量
-
这个问题有什么更新吗?
-
@ZeroOne 添加一个全局变量确实解决了我的问题,但是由于我有一个计时器并且我想将该值作为用户的高分返回,所以我遇到了其他问题。不过谢谢!
标签: java android android-studio