今天在实现一个保存用户设置到SharedPreferences时,出现了一个不能将数据保存到SharedPreferences中的情况。经过仔细的分析得出:

android.content.SharedPreferences.Editor.putString(String key, String value)

和

android.content.SharedPreferences.Editor.commit()

这两个方法所用的editor不同所致。

错误代码如下:

SharedPreferences config_sp = getSharedPreferences("config", MODE_PRIVATE);

config_sp.edit().putString("passwd_safe", enCodeSafePasswd);
config_sp.edit().commit();

SharedPreferences.edit()方法的说明如下

/**
     * Create a new Editor for these preferences, through which you can make
     * modifications to the data in the preferences and atomically commit those
     * changes back to the SharedPreferences object.
     *
     * <p>Note that you <em>must</em> call {@link Editor#commit} to have any
     * changes you perform in the Editor actually show up in the
     * SharedPreferences.
     *
     * @return Returns a new instance of the {@link Editor} interface, allowing
     * you to modify the values in this SharedPreferences object.
     */

正如上面红色的标记所说,该方法会创建一个新的Editor对象,因此两次的Editor对象是不一样的。所以就造成了数据存储失败。

android.content.SharedPreferences.edit()的更多相关文章

  1. Android之SharedPreferences数据存储

    一.SharedPreferences保存数据介绍 如果有想要保存的相对较小键值集合,应使用SharedPreferences API.SharedPreferences对象指向包含键值对的文件并提供 ...

  2. 18.Android之SharedPreferences数据存储学习

    SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...

  3. Android的SharedPreferences实用技巧

    转自:http://blog.csdn.net/jingfeizhu/article/details/10017779 SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XM ...

  4. Android中SharedPreferences和序列化结合保存对象数据

    前言: 最近在做用户注册的时候,遇到了需要填写很多表单数据,不可能在一页把所有的数据都展示完全,因此采用了分页的方式,由于在用户填写数据数据之后我们需要对用户填写的数据进行暂时的记录的,当用户会到此页 ...

  5. Android 之 SharedPreferences

    1 简介 SharedPreferences是一种轻量级的数据存储方式,它可以用键值对的方式把简单数据类型(boolean.int.float.long和String)存储在应用程序的私有目录下(da ...

  6. 学习Android之SharedPreferences使用

    效果图例如以下: 当我们想让自己的属性设置保存下来,这时就须要SharedPreferences. 上面这个小程序,音乐状态是保存下来的.使用的上一次退出的状态. 进入DDMS,data文件下的dat ...

  7. Android 之 SharedPreferences应用

    Android 平台给我们提供了一个 SharedPreferences 类,它是一个轻量级的存储类,特别适合用于保存共享数据.使用SharedPreferences保存数据,其背后是用xml文件存放 ...

  8. Android之SharedPreferences权限

    import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; pub ...

  9. Android 使用SharedPreferences数据存储

    自己写了个SP辅助类 尽管写的有点啰嗦,也是自己的成果.例如以下: package com.yqy.yqy_testsputil; import android.annotation.Suppress ...

随机推荐

  1. IE下默认TD colspan rowspan值为1

    IE下默认TD colspan rowspan值为1,即使这个TD没有合并没有rowspan,colspan属性,其值都为1,chrome下正常. 判断是否rowspan colspan为TD.get ...

  2. ubuntu 安装VmTool

    VM tools 是Vmware的一组工具.主要用于虚拟主机显示优化与调整,另外还可以方便虚拟主机与本机的交互,如允许共享文件夹,甚至可以直接从本机向虚拟主机拖放文件.鼠标无缝切换.显示分辨率调整等, ...

  3. .Net下一个类型转换神器

    引言 类型转换经常遇到,最常用的应该是string类型转换为其它基元类型,常见于http参数类型转换.Convert静态类的Convert.ChangeType()方法可以把实现IConvertibl ...

  4. Angular指令1

    Angular的指令 也就是directive,其实就是一WebComponent,以前端的眼光来看,好象很复杂,但是以后端的眼光来看,还是非常简单的.其实就是一个中等水平的类. var myModu ...

  5. PKI系统深入介绍

    公钥基础设施(Public Key Infrastructure,简称PKI)是目前网络安全建设的基础与核心,是电子商务安全实施的基本保障,因 此,对PKI技术的研究和开发成为目前信息安全领域的热点. ...

  6. CSS3 GRID LAYOUT

    CSS3 GRID LAYOUT http://www.w3cplus.com/blog/tags/356.html 中国首个开源 HTML5 跨屏前端框架 http://amazeui.org/

  7. 《ImageNet Classification with Deep Convolutional Neural Networks》 剖析

    <ImageNet Classification with Deep Convolutional Neural Networks> 剖析 CNN 领域的经典之作, 作者训练了一个面向数量为 ...

  8. xshell4|5远程连接工具

    志同道合,方能谈天说地! 对比其他的工具,对于功能来说xshell是比较厉害的.有能力的可以支持正版! Xshell4 链接: http://pan.baidu.com/s/1jHAgboa 密码: ...

  9. ElasticSearch入门系列(二)交互API

    一.基于HTTP协议,以JSON为数据交互格式的RESTful API 向ElasticSearch发出请求的组成部分与其他的普通的HTTP请求是一样的: curl -X<VERB> '& ...

  10. SharePoint Web Part Error – The Specified Solution Was Not Found

    If you develop, release and add a SharePoint 2010 sandboxed solution web part to a page, then change ...