问题描述
- androidstudio下用sharedreference保存数据
-
写完如下程序后:
public class MainActivity extends AppCompatActivity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button save = (Button) findViewById(R.id.save); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = getSharedPreferences("Data",MODE_PRIVATE).edit(); editor.putInt("age",18); editor.putString("name","jack"); editor.putBoolean("married",false); editor.commit(); } }); }
}
运行时,显示这样的问题:
04-13 13:59:09.591 2142-2142/com.example.admin.sharedpreferencestest W/System: ClassLoader referenced unknown path: /data/app/com.example.admin.sharedpreferencestest-1/lib/x86
04-13 13:59:09.691 2142-2157/com.example.admin.sharedpreferencestest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-13 13:59:09.775 2142-2157/com.example.admin.sharedpreferencestest I/OpenGLRenderer: Initialized EGL, version 1.4
04-13 13:59:09.837 2142-2157/com.example.admin.sharedpreferencestest W/EGL_emulation: eglSurfaceAttrib not implemented
04-13 13:59:09.838 2142-2157/com.example.admin.sharedpreferencestest W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdff880, error=EGL_SUCCESS而且我找了几乎所有地方,都没看见这个sharedreference文件。求大神解惑啊,小弟感激不尽。
解决方案
Share~ Sdf. = getshare
Editor editor= sdf.editor
editor.put
解决方案二:
getSharedPreferences前面把上下文加上,默认是onclick---this。自己加上MainActivity .this.getSharedPreferences
解决方案三:
MODE_PRIVATE
你改成这个试试
getSharedPreferences("Date",MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
而且,我觉得你的sharedprefrences的edit的写和得到sharedprefrences得到,分开写比较不容易出错
解决方案四:
要是不行就这样
getSharedPreferences("Date",7);
解决方案五:
你确定是这一块的问题吗?
你把代码注释一下看看会不会出错~·