Java 为什么共享首选项不保存值?
我在安卓工作室制作了一款应用程序游戏,并对其进行了测试。除了一件事之外,所有的一切似乎都起了作用。也就是说,当我获得一个新的高分时,它要么保存不正确,要么显示不正确Java 为什么共享首选项不保存值?,java,android,sharedpreferences,Java,Android,Sharedpreferences,我在安卓工作室制作了一款应用程序游戏,并对其进行了测试。除了一件事之外,所有的一切似乎都起了作用。也就是说,当我获得一个新的高分时,它要么保存不正确,要么显示不正确 public GamePanel(Context context) { super(context); this.mContext = context; } //display public void drawText(Canvas canvas) { SharedPreferences prefs =
public GamePanel(Context context) {
super(context);
this.mContext = context;
}
//display
public void drawText(Canvas canvas) {
SharedPreferences prefs = mContext.getSharedPreferences("PrefsKeys", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("highScore", 0);
int newScore = Player.getScore() * 3;
//update score only if new score is higher
if (newScore > oldScore) {
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("highScore", 0);
editor.commit();
}
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
canvas.drawText("DISTANCE: " + newScore, 10, HEIGHT - 10, paint);
canvas.drawText("HighScore: " + oldScore, WIDTH - 215, HEIGHT - 10, paint);
想必是命中注定的
editor.putInt("highScore", newScore);
editor.putInt("highScore", newScore);