android:SharedPreference 读取失败的原因及解决办法
2016-06-24 14:25
691 查看
如下代码:private int typeLastItem = 0;private int areaLastItem = 0;private int timeLastItem = 0;
读取数据:SharedPreferences sp = getSharedPreferences("yst_filter", Context.MODE_PRIVATE);typeLastItem = sp.getInt("yst_filter_type", -1);areaLastItem = sp.getInt("yst_filter_area", -1);timeLastItem = sp.getInt("yst_filter_time", -1);
写入数据:SharedPreferences sp = getSharedPreferences("yst_filter", Context.MODE_PRIVATE);sp.edit().putInt("yst_filter_type", 1);sp.edit().putInt("yst_filter_area", 2);sp.edit().putInt("yst_filter_time", 3);sp.edit().commit();
以上代码,不管怎么读取,三个值均为默认值-1,打印log,显示sp.edit().commit()结果为true,但就是读取值为默认值-1。
原因:就出在下面这几句代码这里sp.edit().putInt("yst_filter_type", 1);sp.edit().putInt("yst_filter_area", 2);sp.edit().putInt("yst_filter_time", 3);sp.edit().commit();四句代码相当于四个Editor mEditor = sp.edit();前面三个只是putInt,并没有commit,第四个有commit,但没有put,所以读取的值均为默认值。结论:上面四句写法是错误的。
解决办法:将上面四句改为以下两种方法中的任何一种均可:第一种:Editor edit = sp.edit();edit.putInt("yst_filter_type", typeLastItem);edit.putInt("yst_filter_area", areaLastItem);edit.putInt("yst_filter_time", timeLastItem);edit.commit();
第二种:sp.edit().putInt("yst_filter_type", typeLastItem) .putInt("yst_filter_area", areaLastItem) .putInt("yst_filter_time", timeLastItem).commit();
读取数据:SharedPreferences sp = getSharedPreferences("yst_filter", Context.MODE_PRIVATE);typeLastItem = sp.getInt("yst_filter_type", -1);areaLastItem = sp.getInt("yst_filter_area", -1);timeLastItem = sp.getInt("yst_filter_time", -1);
写入数据:SharedPreferences sp = getSharedPreferences("yst_filter", Context.MODE_PRIVATE);sp.edit().putInt("yst_filter_type", 1);sp.edit().putInt("yst_filter_area", 2);sp.edit().putInt("yst_filter_time", 3);sp.edit().commit();
以上代码,不管怎么读取,三个值均为默认值-1,打印log,显示sp.edit().commit()结果为true,但就是读取值为默认值-1。
原因:就出在下面这几句代码这里sp.edit().putInt("yst_filter_type", 1);sp.edit().putInt("yst_filter_area", 2);sp.edit().putInt("yst_filter_time", 3);sp.edit().commit();四句代码相当于四个Editor mEditor = sp.edit();前面三个只是putInt,并没有commit,第四个有commit,但没有put,所以读取的值均为默认值。结论:上面四句写法是错误的。
解决办法:将上面四句改为以下两种方法中的任何一种均可:第一种:Editor edit = sp.edit();edit.putInt("yst_filter_type", typeLastItem);edit.putInt("yst_filter_area", areaLastItem);edit.putInt("yst_filter_time", timeLastItem);edit.commit();
第二种:sp.edit().putInt("yst_filter_type", typeLastItem) .putInt("yst_filter_area", areaLastItem) .putInt("yst_filter_time", timeLastItem).commit();
相关文章推荐
- Android:DownloadManager setDestinationInExternalPublicDir(subPath, fileName)使用说明
- Android:解决列表滚动时背景色变黑的方法
- android:报Activity has leaked IntentReceiver或者receiver is not registered错误
- android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
- Android:apk签名
- Android:判断某一个service是否已经在运行
- android:根据下载文件的url获取文件名
- Android:monkey测试工具
- Android java.lang.RuntimeException: Can\'t create handler inside thread that has not called Looper.prepare()
- Android:adb 启动activity、service,发送broadcast
- Android 防止Service被系统回收
- Android 点击back键两次退出程序
- Android 判断一个Service是否已经运行
- Android初学感受一
- android函数调用堆栈打印方法
- Android:View的测量/onMeasure()方法解析
- 百度定位Sdk 162错误解决方法之Android Studio
- Android 之 打包混淆ProGuard
- Android 内存泄漏总结
- 小米推送简单使用方法