android - 使用系统偏好设置存储值(value)

标签 android sharedpreferences

我正在尝试使用系统首选项存储一些变量。我尝试了一些教程,但应用程序不断崩溃。有人可以帮助我吗。我也根据 this 进行了尝试.还有这段代码:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    Editor editor = sharedPreferences.edit();

这是 logcat 输出:

06-03 15:17:08.600: D/BstCommandProcessor-Application(908): Application crash has been observed. 
06-03 15:17:08.600: I/Process(10216): Sending signal. PID: 10216 SIG: 9
06-03 15:17:08.600: D/AndroidRuntime(10216): procName from cmdline: com.example.flash4
06-03 15:17:08.600: E/AndroidRuntime(10216): in writeCrashedAppName, pkgName :com.example.flash4
06-03 15:17:08.600: D/AndroidRuntime(10216): file written successfully with content: com.example.flash4 StringBuffer : ;com.example.flash4
06-03 15:17:08.600: E/AndroidRuntime(10216): FATAL EXCEPTION: main
06-03 15:17:08.600: E/AndroidRuntime(10216): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.flash4/com.example.flash4.MainActivity}: java.lang.NullPointerException
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.os.Looper.loop(Looper.java:137)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at java.lang.reflect.Method.invoke(Method.java:511)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at dalvik.system.NativeStart.main(Native Method)
06-03 15:17:08.600: E/AndroidRuntime(10216): Caused by: java.lang.NullPointerException
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at com.example.flash4.MainActivity.<init>(MainActivity.java:71)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at java.lang.Class.newInstanceImpl(Native Method)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at java.lang.Class.newInstance(Class.java:1319)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
06-03 15:17:08.600: E/AndroidRuntime(10216):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
06-03 15:17:08.600: E/AndroidRuntime(10216):    ... 11 more
06-03 15:17:08.690: I/ActivityManager(610): Process com.example.flash4 (pid 10216) has died.
06-03 15:17:08.690: W/ActivityManager(610): Force removing ActivityRecord{b335c7d8 com.example.flash4/.MainActivity}: app died, no saved state

最佳答案

尝试以下代码:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

而不是:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

编辑

如果您尚未执行此操作,请将上述代码放入 onCreate() 方法中。如果您希望首选项在其他地方可用,请执行

SharedPreferences sharedPreferences;

以便将其声明为全局,然后在 onCreate()

中执行此操作
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

关于android - 使用系统偏好设置存储值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012090/

相关文章:

java - 将值插入 android sqlite 数据库的最快方法

java - 为什么我得到默认值?

android - 为 "one row"数据使用 sharedPreferences 或数据库

android - 将 Bundle 保存到 SharedPreferences

java - Trig AlertDialog 我们选中复选框

android - 在 Android 中从互联网获取数据

android - 如何在第一个 View FrameLayout 上禁用监听器和输入

java - Java中的形状模式匹配算法

java - Android SharedPreferences getInt 异常

android - 根据输入调出句子