首选项始终返回默认值

时间:2015-10-14 11:23:30

标签: java android sharedpreferences android-sharedpreferences

此代码用于在SharedPreference中创建值。

SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
Editor editor = sharedpreferences.edit();
editor.putInt("ans1",1).commit();

以下是从SharedPreference获取值的代码。

SharedPreferences sharedpreferences=getApplicationContext().getSharedPreferences("user", Activity.MODE_PRIVATE);
int ans = sharedpreferences.getInt("ans1",7);

ans的值总是7。

0 个答案:

没有答案
相关问题