使用GSON时遇到问题。
首先,每当我切换活动时,我都会尝试使用GSON在SharedPreferences中保存一个Object。然后我的计划是在我回到活动后再次获取该对象,以便我可以重新填充该页面上捕获的数据。但是,我的问题是我的onPause方法发生错误。这个错误已经困扰了我2天,我希望你们能帮忙。我非常需要它。
这是我的onPause和onResume代码:
@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();
}
}
这是我得到的错误日志:
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)
非常感谢所有帮助。非常感谢提前。
**编辑 ** ConnectionObject是一个存储这些值的简单对象:
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;
答案 0 :(得分:1)
我解决了它,虽然我认为它只是一个循环的想法。我只是将我的对象分成两部分。第一个是String和int值,第二个是列表。我分别存储了两个部分,然后在简历上再次组合它们。谢谢你的想法。这是很多帮助。