2

I am trying to store some variables using system preferences.I tried some tutorials, but the application keeps on crashing.Can someone help me out.I also tried according to this.Also this code :

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

This is the logcat output:

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
Community
  • 1
  • 1
jincy abraham
  • 579
  • 2
  • 9
  • 21
  • Which code do you need.Error came at line 71 ie,the code that i gave above. – jincy abraham Jun 03 '14 at 09:58
  • 1
    try this `SharedPreferences sharedPreferences =getDefaultSharedPreferences(this);` in your `Activity` – M D Jun 03 '14 at 10:00
  • have You renamed anything in Your project? IF yes, be sure You have no errors inside ande clean project – Opiatefuchs Jun 03 '14 at 10:00
  • @Simple Plan:that line is giving me this error : The method getDefaultSharedPreferences(MainActivity) is undefined for the type MainActivity – jincy abraham Jun 03 '14 at 10:04
  • 2
    Make sure the context you give is valid. In order to get the default SharedPreferences you need to have an context. This is required because getDefaultSharedPreferences() will call getSharedPreferences(String name, mode), where "name" is gotten like this, context.getPackageName() + "_preferences"; You can also make your custom file, like: SharedPreferences sharedPreferences = context.getSharedPreferences("sp_file_name", Context.MODE_PRIVATE); – Ionut Negru Jun 03 '14 at 10:07
  • Thanks.it worked.I gave the code inside the onCreate() and it worked :) – jincy abraham Jun 03 '14 at 10:21

2 Answers2

3

Try the following code:

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

Instead of:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

EDIT

If you are not already doing this, place the above code in the onCreate() method. If you want the preferences to be available elsewhere, do

SharedPreferences sharedPreferences;

so that it gets declared as global, then do this in onCreate()

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Harikrishnan
  • 7,765
  • 13
  • 62
  • 113
2

As Simple you can use this.

SharedPreferences sharedPreferences = getSharedPreferences("MyKey",0);
Editor editor = sharedPreferences.edit();

and when you want to retrieve then you use this wherever you want in your app

SharedPreferences sharedPreferences = getSharedPreferences("MyKey",0);
Piyush
  • 18,895
  • 5
  • 32
  • 63