我正在制作一款需要将一些统计数据保存到记忆中的游戏(玩游戏,赢取游戏等)。 我曾试图使用共享偏好,但似乎从我能看到的内容中找不到文档,而且我还没有能够使用它。 我错过了什么吗?
try {
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
SharedPreferences stats = getPreferences(MODE_PRIVATE);
CharSequence gampjla = "Games played: " + stats;
Toast toast = Toast.makeText(context, gampjla, duration);
toast.show();
gamesPlayed++;
SharedPreferences.Editor editor = stats.edit();
editor.putInt("gameplay", gamesPlayed).commit();
CharSequence gampla = "Games played: " + gamesPlayed;
Toast toayst = Toast.makeText(context, gampla, duration);
toayst.show();
}
catch (Exception e) {
Context context = getApplicationContext();
CharSequence errormsg = "Unable to save!! " + e;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, errormsg, duration);
toast.show();
}
我尝试过各种变化,但没有任何效果! 谢谢你的帮助!
编辑:一些变量名中的随机字母是因为我想要一个不同的变量名来进行测试,因为我感到很沮丧,所以我不能在这一点上烦恼!答案 0 :(得分:0)
改变这个:
CharSequence gampjla = "Games played: " + stats;
为:
CharSequence gampjla = "Games played: " + stats.getInt("gameplay",-1);
答案 1 :(得分:0)
解决! 这是一个愚蠢的问题。我曾写过' putInt'但使用了' getString'。