I have a problem when I use GSON.
First off I’m trying to save an Object inside SharedPreferences with the use of GSON, whenever I switch activities. Then my plan was to get that object again once I get back to the activity so that I can repopulate the captured data on that page. But, my problem is that an error occurs on my onPause method. This error has already been bugging me for 2 days, I hope you guys can help. I seriously need it.
Here is my onPause and onResume code:
@Override
protected void onPause() {
super.onPause();
try {
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
Editor prefsEditor = appSharedPrefs.edit();
activityDataContainer = convertDataContainers(); //This just gets the data from the page
prefsEditor.putInt("objectLength", activityDataContainer.size());
Gson gson = new Gson();
for(int x = 0; x < activityDataContainer.size(); x++){
ConnectionObject temp = activityDataContainer.get(x);
String json = gson.toJson(temp);
prefsEditor.putString("connectionData"+x, json);
}
prefsEditor.commit();
Toast.makeText(this, "Object stored in SharedPreferences", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
};
@Override
protected void onResume() {
super.onResume();
//===== Using SharedPreferences
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
if(appSharedPrefs.contains("objectLength")){
int length = appSharedPrefs.getInt("objectLength", 0);
activityDataContainer = new ArrayList<ConnectionObject>();
Gson gson = new Gson();
for(int x = 0; x < length; x++){
String json = appSharedPrefs.getString("connectionData"+x, "");
ConnectionObject object = gson.fromJson(json, ConnectionObject.class);
activityDataContainer.add(object);
}
revertDataContainers(activityDataContainer); //Repopulates the data
Toast.makeText(this, "Object retrieved in SharedPreferences", Toast.LENGTH_SHORT).show();
}
}
Here is the Error Log I get:
05-10 02:14:47.822: E/AndroidRuntime(31718): FATAL EXCEPTION: main
05-10 02:14:47.822: E/AndroidRuntime(31718): java.lang.RuntimeException: Unable to pause activity {com.NelConsulting.FieldCaptureTool/com.MyClass}: java.lang.IllegalArgumentException: class android.database.sqlite.SQLiteDatabase declares multiple JSON fields named mLock
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2354)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2311)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2291)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.access$1700(ActivityThread.java:117)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:938)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.os.Looper.loop(Looper.java:130)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 02:14:47.822: E/AndroidRuntime(31718): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 02:14:47.822: E/AndroidRuntime(31718): at java.lang.reflect.Method.invoke(Method.java:507)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 02:14:47.822: E/AndroidRuntime(31718): at dalvik.system.NativeStart.main(Native Method)
05-10 02:14:47.822: E/AndroidRuntime(31718): Caused by: java.lang.IllegalArgumentException: class android.database.sqlite.SQLiteDatabase declares multiple JSON fields named mLock
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:122)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.getAdapter(Gson.java:349)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.getAdapter(Gson.java:349)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:52)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.getAdapter(Gson.java:349)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.(ReflectiveTypeAdapterFactory.java:82)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.getAdapter(Gson.java:349)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:55)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.toJson(Gson.java:582)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.toJson(Gson.java:561)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.toJson(Gson.java:516)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.google.gson.Gson.toJson(Gson.java:496)
05-10 02:14:47.822: E/AndroidRuntime(31718): at com.MyClass.onPause(ConnectionsActivity.java:804)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.Activity.performPause(Activity.java:3851)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
05-10 02:14:47.822: E/AndroidRuntime(31718): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2341)
All help will be greatly appreciated. Much thanks in advance.
**EDIT
**ConnectionObject is a simple object that stores these values:
private boolean toggleButtonValue;
private int sdGenValue;
private int sdSpecValue;
private int sdCountValue;
private String sdLengthValue;
private int sdCondValue;
private int sdOwnValue;
private String sdDateValue;
private String mLtValue;
private String mLgValue;
private String mAcValue;
private int mPhotoValue;
private List<MeterObject> meterContainer;
I solved it, though I think its just a loop around idea. I just split my object into 2 parts. The first being the String and int values, and the second one being the list. I stored both parts separately then combine them again on resume. Thanks for the ideas guys. It was a lot of help.