我是使用共享偏好的新手,在我第一次尝试时,我遇到了对我来说没有意义的错误。我分配一个这样的值:
int saveScore = sp.getInt("SAVE_SPOT",0); //This is intentional to get the
//default value of 0 to go to case 0
switch(saveScore){
case 0:
SharedPreferences.Editor edit1 = sp.edit();
edit1.putInt("SCORE_1", score);
edit1.putInt("SAVE_SPOT", 1);
edit1.commit();
break;
case 1:
int previous_score = sp.getInt("SCORE_1",0); // error happens here
if(sp.getInt("SCORE_1",0)>score){
SharedPreferences.Editor edit2 = sp.edit();
edit2.putInt("SCORE_2", score);
edit2.putInt("SAVE_SPOT", 2);
edit2.commit();
}
else{
SharedPreferences.Editor edit3 = sp.edit();
edit3.putInt("SCORE_2", previous_score);
edit3.putInt("SCORE_1", score);
edit3.putInt("SAVE_SPOT", 1);
edit3.commit();
}
break;
每次我运行程序时,我都会收到错误“字符串不能转换为整数”。我几乎 99% 确定变量 score 是 int 而不是字符串,但我不确定为什么会收到此错误。