当前位置: 首页 > 知识库问答 >
问题:

java.lang.AssertionError:不可能由:java.lang.NosuchMethodException:values[]引起

贺兴昌
2023-03-14

怎么了:

  1. 我有多个对象保存在一个arraylist中,该arraylist保存在一个gson字符串中。
  2. 我更改对象的类(以包含更多变量和getter/setter方法)
  3. 现在我在尝试加载较旧的对象(保存在步骤1中)时遇到错误
    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) 

谢谢你的帮助!

共有1个答案

陈欣荣
2023-03-14

参见Android release APK crash with java.lang.AssertionError:在java.lang.Enum中不可能,在Gson EnumTypeAdapter中AssertionError时使用Proguard模糊处理或https://github.com/zendesk/sunshine-conversations-android/issues/93。在proguard-rules.pro中添加:

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
 类似资料:
  • 我想将转换流写入Elasticsearch索引,如下所示: 行抛出错误(见下文)。我尝试了不同的方法来解决这个问题(例如,在旁边添加),但似乎没有任何效果。 它是否与Hadoop的配置有关?(我参考了以下消息:) 更新:

  • 我目前正在将我的一个Java项目从JDK1.5.0_14更新到JDK8u202。 这是我的问题: > 我可以使用 JDK 1.5.0_14 和 JDK 8u202 构建项目。 但是一旦我更改了一行代码(不管是什么),我就会得到以下java.lang.AssertionError(参见这篇文章的末尾)。 当我降级到 JDK 1.5.0_14,构建项目一次并升级到 JDK 8u202 时,我可以再次构

  • 我想检查文本字段值是否与我在代码中提到的预期值相同 这是我需要获取值的文本字段 输入类型=“文本”值=“萨达斯”类=“mdl-textfield__input”id=“last_name”名称=“last_name”占位符=“输入姓氏” 下面提到的代码得到了 AssertionError, Assert.assertEquals(driver.findElement(By.xpath(“//inp

  • 我知道“接口”和“抽象类”的区别。但是你能给我提供一个可以通过“接口”而不是通过“抽象类”构建的例子吗,留下了“多重继承”的例子?

  • 当我试图运行下面的代码时,每次我都得到错误“org.openqa.selenium.timeoutexception:Expected condition failed:waiting for visibility of element Locited by by.xpath”,我不确定xpate会是什么,请有人帮助我为登录应用程序创建自动化测试用例。 Java: HTML: