I'm about to write a small app where I have pretty much all the data stored in a custom array which contains of list views. I don't wanna save the data in a different form. This array I like to save in shared preferences. I head a look for other answered questions and tried different ways but none of then worked for me.
In the main activity, I have two small methods:
public class MainActivity extends AppCompatActivity {
public static ArrayList<Item> items = new ArrayList<Item>();
SharedPreferences mPrefs = getBaseContext().getSharedPreferences("KEY12", Context.MODE_PRIVATE);
Gson gson = new Gson();
[...]
protected void savePrefs() {
SharedPreferences.Editor editor = mPrefs.edit();
String json = gson.toJson(items);
System.out.println(json);
editor.putString("items_prefs", json);
editor.commit();
}
protected void loadPrefs() {
String empty_list = gson.toJson(new ArrayList<Item>());
items = gson.fromJson(mPrefs.getString("KEY12", empty_list),
new TypeToken<ArrayList<Item>>() {}.getType());
}
}
I'm calling the loadPrefs();
method in onCreate and savePrefs();
in onResume so everytime when I come back to the main activity, the preferences should be updated.
The error that I get is:
FATAL EXCEPTION: main Process: com.example.android.calc123, PID: 30579 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.calc123/com.example.android.calc123.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.and roid.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference at com.example.android.calc123.MainActivity.(MainActivity.java:61) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
How can I fix this problem or - asking in a different way - what am I doing wrong when dealing with shared preferences?
As the error log say you call getSharedPreferences in a null pointer (getBaseContext)
put in OnCreate mPrefs = getBaseContext().getSharedPreferences("KEY12", Context.MODE_PRIVATE);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.