gson java.lang.AssertionError:不可能原因:java.lang.NoSuchMethodException:值[]

djmepvbi  于 2022-11-06  发布在  Java
关注(0)|答案(2)|浏览(220)

发生了什么事

1.我有多个对象保存在一个数组列表中,而数组列表保存在一个gson字符串中。
1.我更改了对象的类(以包含更多变量和getter/setter方法)
1.现在,我在尝试加载旧对象(在步骤1中保存)时遇到错误

需要考虑的事项:

我使用gson/json将数组列表存储在一个android共享首选项中。(共享首选项对其他值工作正常,错误似乎是在使用json/gson将字符串转换为数组列表时)

我的密码:

public void loadDataSavedMatches() {
    SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
    Gson gson = new Gson();
    String json = prefs.getString("task list", null);
    Type type = new TypeToken<ArrayList<TennisMatch>>() {
    }.getType();
    mTennisMatches = gson.fromJson(json, type);

    if (mTennisMatches == null) {
        mTennisMatches = new ArrayList<>();
    }

    Log.i("LOAD DATA", String.valueOf(mTennisMatches));
    Log.i("LOAD DATA2", String.valueOf(prefs));

    //hashMap

    SharedPreferences hashPrefs = getSharedPreferences(HASH_MAP_SHARED_PREFS_FILE, Context.MODE_PRIVATE);
    Gson hashGson = new Gson();
    String hashJson = hashPrefs.getString("hashString", null);
    Type hashType = new TypeToken<HashMap<String, TennisMatch>>() {
    }.getType();
    item_map = hashGson.fromJson(hashJson, hashType);

    if (item_map == null) {
        item_map = new HashMap<String, TennisMatch>();
    }

}

错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.myapp, PID: 28341
java.lang.AssertionError: impossible
    at java.lang.Enum$1.create(Enum.java:45)
    at java.lang.Enum$1.create(Enum.java:35)
    at libcore.util.BasicLruCache.get(BasicLruCache.java:54)
    at java.lang.Enum.getSharedConstants(Enum.java:211)
    at java.lang.Class.getEnumConstants(Class.java:1063)
    at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(TypeAdapters.java:778)
    at com.google.gson.internal.bind.TypeAdapters$30.create(TypeAdapters.java:817)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164)
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
    at com.google.gson.Gson.getAdapter(Gson.java:423)
    at com.google.gson.Gson.fromJson(Gson.java:887)
    at com.google.gson.Gson.fromJson(Gson.java:853)
    at com.google.gson.Gson.fromJson(Gson.java:802)
    at com.android.myapp.Activity.loadDataSavedMatches(ItemMatchListActivity.java:282)
    at com.android.myapp.Activity.onCreate(ItemMatchListActivity.java:87)
    at android.app.Activity.performCreate(Activity.java:6102)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    at android.app.ActivityThread.access$1200(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5593)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 Caused by: java.lang.NoSuchMethodException: values []
    at java.lang.Class.getMethod(Class.java:664)
    at java.lang.Class.getDeclaredMethod(Class.java:626)
    at java.lang.Enum$1.create(Enum.java:41)
    at java.lang.Enum$1.create(Enum.java:35) 
    at libcore.util.BasicLruCache.get(BasicLruCache.java:54) 
    at java.lang.Enum.getSharedConstants(Enum.java:211) 
    at java.lang.Class.getEnumConstants(Class.java:1063) 
    at com.google.gson.internal.bind.TypeAdapters$EnumTypeAdapter.<init>(TypeAdapters.java:778) 
    at com.google.gson.internal.bind.TypeAdapters$30.create(TypeAdapters.java:817) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) 
    at com.google.gson.Gson.getAdapter(Gson.java:423) 
    at com.google.gson.Gson.fromJson(Gson.java:887) 
    at com.google.gson.Gson.fromJson(Gson.java:853) 
    at com.google.gson.Gson.fromJson(Gson.java:802) 
    at com.android.myapp.Activity.loadDataSavedMatches(ItemMatchListActivity.java:282) 
    at com.android.myapp.Activity.onCreate(ItemMatchListActivity.java:87) 
    at android.app.Activity.performCreate(Activity.java:6102) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
    at android.app.ActivityThread.access$1200(ActivityThread.java:165) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5593) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

感谢您的帮助

1bqhqjot

1bqhqjot2#

我加入的一些新数据是Firebase DocumentReferences和CollectionReferences,我注意到这些数据类型不适合gson/json,因此我将它们排除在外。我正在考虑使用字符串来保存文档和集合引用。

//    private CollectionReference mColRef; //I removed this from my Object 
//    private DocumentReference mDocRef;

相关问题