我正在尝试使用系统偏好来存储一些变量。我尝试了一些教程,但是应用程序一直在崩溃。有人帮助我。我也尝试了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
答案 0 :(得分:3)
请尝试以下代码:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
而不是:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
修改强>
如果您还没有这样做,请将上述代码放在onCreate()
方法中。如果您希望其他地方可以使用偏好设置,请执行
SharedPreferences sharedPreferences;
以便将其声明为全局,然后在onCreate()
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
答案 1 :(得分:2)
简单易用就可以了。
SharedPreferences sharedPreferences = getSharedPreferences("MyKey",0);
Editor editor = sharedPreferences.edit();
当您想要检索时,您可以在应用中的任何位置使用此
SharedPreferences sharedPreferences = getSharedPreferences("MyKey",0);