在android studio中尝试使用gson保存arraylist时出错

kg7wmglp  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(377)

在我正在编程的android应用程序中,我试图使用gson库保存带有自定义对象的arraylist。如果我称之为

private void saveData(){
    SharedPreferences sharedPreferences = getSharedPreferences("shared preferences",MODE_PRIVATE);
    SharedPreferences.Editor editor =  sharedPreferences.edit();
    Gson gson = new Gson();
    String json = gson.toJson(recipe_items);
    editor.putString("task list",json);
    editor.apply();
}

保存功能,我的应用程序崩溃,并显示以下错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yzcorp.kochapp, PID: 19949
java.lang.IllegalArgumentException: class com.samsung.android.directpeninput.PopupCue$CueContainer declares multiple JSON fields named mContext
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
    at com.google.gson.Gson.getAdapter(Gson.java:458)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
    at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107)
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:97)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:61)
    at com.google.gson.Gson.toJson(Gson.java:704)
    at com.google.gson.Gson.toJson(Gson.java:683)
    E/AndroidRuntime:     at com.google.gson.Gson.toJson(Gson.java:638)
    at com.google.gson.Gson.toJson(Gson.java:618)
    at com.yzcorp.kochapp.Recipescroll.saveData(Recipescroll.java:227)
    at com.yzcorp.kochapp.Recipescroll.access$200(Recipescroll.java:55)
    at com.yzcorp.kochapp.Recipescroll$1$3.onCancel(Recipescroll.java:158)
    at android.app.Dialog$ListenersHandler.handleMessage(Dialog.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7156)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)

RecipeCroll是我的主要活动类,recipe_items是带有自定义对象的arraylist。
这是保存在arraylist中的类

public class recipe_item {
private Drawable mImageResource;
private String time;
private String title;
private String manual;
private ArrayList<Ingredient> ingredients;
public recipe_item(Drawable ImageResource,String time1, String title1,String manual1 , ArrayList<Ingredient> ingredients1){
    mImageResource = ImageResource;
    time = time1;
    title = title1;
    manual = manual1;
    ingredients = ingredients1;
}
public Drawable getmImageResource(){
    return mImageResource;
}
public String getTime(){
    return time;
}
public String getTitle(){
    return title;
}
public String getManual() { return manual; }
public ArrayList<Ingredient> getIngredients() {
    return ingredients;
}

}
我已经试过了
将所有对话框字段设置为 transient
使用排除策略跳过名为mcontext的字段,但它只会给我带来更多错误
我不能做什么
更改popupcue类的代码,因为它是随android提供的
(sry表示英语不好,我很乐意回答)
我在网上找到了什么
这可能就是我正在处理的类:https://github.com/fonix232/scoverre/blob/master/reversed/framework/com/samsung/android/directpeninput/popupcue.java

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题