简体   繁体   English

SharedPreferences有一些问题

[英]SharedPreferences have some problems

I create a mini game . 我创造了一个迷你游戏。 The User need to answer my question in 2 seconds, if user answer true , the score will increase 1 , and if user answer wrong - Game over and the score become to 0. 用户需要在2秒内回答我的问题,如果用户回答“是”,则分数将增加1,如果用户回答错误,则游戏结束,分数将变为0。

I want to use SharedRefences so that I can save the score when the user play again . 我想使用SharedRefences以便当用户再次玩游戏时可以保存分数。 But it's not working . 但这不起作用。

My CountDownTimer: 我的CountDownTimer:

public void start_game() {
    flag = false;
    btnTrue.setClickable(true);
    btnWrong.setClickable(true);
    a = (int) (Math.random() * 6);
    b = (int) (Math.random() * 6);
    sum = (int) (Math.random() * 11);
    tvDetails.setText(a + " + " + b + " = " + sum);

    proTime.setSecondaryProgress(0);
    timer = new CountDownTimer(3000, 10) {

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub

            btnTrue.setClickable(false);
            btnWrong.setClickable(false);
            if (flag) {
                tmp++;
                tvScore.setText("" + tmp);
                start_game();
            } else {
                ShowDialogs();
                tmp = 0;
                tvScore.setText("" + tmp);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            proTime.setSecondaryProgress(proTime.getSecondaryProgress() + 10);
        }
    };

    timer.start();
}


public void ShowDialogs() {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setCanceledOnTouchOutside(true);
    dialog.setCancelable(false);

    tvNowScore = (TextView) dialog.findViewById(R.id.NowScore);
    tvHighScore = (TextView) dialog.findViewById(R.id.HighScore);
    ImageView btnRep = (ImageView) dialog.findViewById(R.id.Rep);
    ImageView btnExit = (ImageView) dialog.findViewById(R.id.Exit);

    ResPreferences();

    int HighScore = Integer.parseInt(tvHighScore.getText().toString());

    int Score = Integer.parseInt(tvScore.getText().toString());

    if (Score > HighScore) {
        HighScore = Score;
        SavingPreferences();
    }

    tvNowScore.setText("" + Score);
    tvHighScore.setText("" + HighScore);

    btnExit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            System.exit(0);

        }
    });


    btnRep.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.cancel();
            if (timer != null) {
                timer.cancel();
                timer = null;
            }
            start_game();
        }
    });
    dialog.show();
}

I can't save a score when the game finish. 游戏结束时我无法保存分数。

This is my Save Preferences 这是我的保存首选项

public void SavingPreferences() {
    SharedPreferences pre = getSharedPreferences(PrefName, MODE_PRIVATE);
    SharedPreferences.Editor editor = pre.edit();

    String HighScore = tvHighScore.getText().toString();
    editor.putString("HighScore", HighScore);
    editor.commit();
}

This is Restrore Pre: 这是Restrore Pre:

public void ResPreferences() {
    SharedPreferences pre = getSharedPreferences(PrefName, MODE_PRIVATE);
    String score = pre.getString("HighScore", "0");
    tvHighScore.setText(score);
}

Try this : 尝试这个 :

 public void SavingPreferences() {
    SharedPreferences pre = getSharedPreferences(PrefName, MODE_PRIVATE);
    String score = null;
    if(pre.contains("HighScore")){
        score = pre.getString("HighScore", "0");
    }
    SharedPreferences.Editor editor = pre.edit();

    String HighScore = tvHighScore.getText().toString();

    if(score != null && Integer.parseInt(score) > Integer.parseInt(HighScore )){
        editor.putString("HighScore", score);
    }  else {
        editor.putString("HighScore", HighScore);
    }

    editor.commit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM