java.lang.AssertionError:不可能引起:java.lang.NoSuchMethodException:值[]
Posted
技术标签:
【中文标题】java.lang.AssertionError:不可能引起:java.lang.NoSuchMethodException:值[]【英文标题】:java.lang.AssertionError: impossible Caused by: java.lang.NoSuchMethodException: values [] 【发布时间】:2019-07-17 18:44:45 【问题描述】:发生了什么:
-
我有多个对象保存在一个数组列表中,该数组列表保存在一个 gson 字符串中。
我更改了对象的类(以包含更多变量和 getter/setter 方法)
现在我在尝试加载旧对象时遇到错误(保存在第 1 步中)
需要考虑的事项:
我使用 gson/json 将 arraylist 存储在 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)
感谢您的帮助!
【问题讨论】:
我认为一种方法是让两个对象类都可用。然后放置一个带有新首选项的属性,如果存在将指示使用哪种对象类型来解析数据。使用旧对象类型解析旧数据,使用新对象类型读取新数据。 另一种选择是使用旧对象类型读取旧首选项并使用新对象类型保存。 这可以作为安卓应用升级的一部分来完成 @muasif80 你认为将旧对象重命名为 oldObjectName 会好吗?还是我应该保留这个名字?我还能在 Arraylist 中包含新旧对象吗? 【参考方案1】:请参阅Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum、AssertionError in Gson EnumTypeAdapter when using Proguard Obfuscation 或 https://github.com/zendesk/sunshine-conversations-android/issues/93。在proguard-rules.pro
添加:
-keepclassmembers enum *
public static **[] values();
public static ** valueOf(java.lang.String);
【讨论】:
【参考方案2】:我包含的一些新数据是 Firebase DocumentReferences 和 CollectionReferences,我注意到这些数据类型不能很好地与 gson/json 配合使用,因此我将它们排除在外。我正在考虑使用字符串来保存文档和集合引用。
// private CollectionReference mColRef; //I removed this from my Object
// private DocumentReference mDocRef;
【讨论】:
这对我有用,虽然我不能 100% 确定这是否是错误的原因。 有谁知道为什么在对象中使用 CollectionReference 和 Document Reference 不起作用? - 谢谢以上是关于java.lang.AssertionError:不可能引起:java.lang.NoSuchMethodException:值[]的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.AssertionError:找到的值显示为空
mockMVC 方法 GET java.lang.AssertionError:预期状态:200 实际:500
更新到 Android Studio 4.1 时出现 java.lang.AssertionError
Mvn clean package 给出了一个意外的 java.lang.AssertionError
EasyMock set方法报错: java.lang.AssertionError
AndroidX.Test ActivityScenario: java.lang.AssertionError: Activity 永远不会变为请求状态“[RESUMED]”(最后一个生命周期转换