1
SharedPreferences peepsScores= PreferenceManager.getDefaultSharedPreferences(GamePlayFirst.this);
SharedPreferences.Editor editor =peepsScores.edit();

editor.putInt("userScore1", zero);
editor.putInt("userScore2", zero);
editor.putInt("userScore3", zero);
editor.putInt("userScore4", zero);
editor.putInt("userScore5", zero);
editor.putInt("userScore6", zero);
editor.putInt("userScore7", zero);
editor.putInt("userScore8", zero);
editor.commit();

SharedPreferences peepsScores = PreferenceManager.getDefaultSharedPreferences(this);
//tryed this
int userScore1 = peepsScores.getInt("userScore1","u");
//tryed this
userScore2 = peepsScores.getInt("userScore2","0");
//and tryed this
String userScore3 = peepsScores.getInt("userScore3","0");
String userScore4 = peepsScores.getInt("userScore4","0");
String userScore5 = peepsScores.getInt("userScore5","0");
String userScore6 = peepsScores.getInt("userScore6","0");
String userScore7 = peepsScores.getInt("userScore7","0");
String userScore8 = peepsScores.getInt("userScore8","0");

无论我更改什么随机内容,getint 方法都带有下划线,然后如果我删除接收类的第一行 peepScore 带有下划线,我一直在 android dev webiste 这个网站和谷歌上,一切都说它像 getInt 一样简单我在做什么错误的

稍后我会尝试弄清楚添加,我想当我最终得到这个时,我会将这个发布给像我这样的所有新手,看看哈哈

4

1 回答 1

13

试试这个。

int userScore1 = peepsScores.getInt("userScore1",0);

当您使用 getInt 时,如果您的值未在共享首选项中设置,则必须返回整数值。

于 2012-10-30T12:00:05.010 回答