为什么共享首选项不保存值?

Kni*_*ork -4 java android sharedpreferences

我一直在Android工作室制作应用程序(游戏)并对其进行测试,但这一切似乎都有效.也就是说,当我获得新的高分时,它要么没有正确保存,要么没有正确显示.

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);
Run Code Online (Sandbox Code Playgroud)

Pau*_*ton 6

editor.putInt("highScore", 0);
Run Code Online (Sandbox Code Playgroud)

大概是有意义的

editor.putInt("highScore", newScore);
Run Code Online (Sandbox Code Playgroud)