gpt4 book ai didi

java - Android 和 GSON 抛出 No-Args Constructor not found 异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:19 25 4
gpt4 key购买 nike

我的 Android 应用程序中出现 GSON 问题。

JSON 输入

[
{"id":"1202","parent_id":"0","agrpcount":"0","disp_order":"1","code":"PROD-NEW","name":"New Products","active":"1"},
{"id":"1119","parent_id":"0","agrpcount":"0","disp_order":"2","code":"PROD-VID","name":"\\\"How To\\\" Videos","active":"1"},
{"id":"1685","parent_id":"0","agrpcount":"0","disp_order":"3","code":"SUPP-RESOUR","name":"Support Resources","active":"1"},
{"id":"49","parent_id":"0","agrpcount":"0","disp_order":"109","code":"EQ","name":"Equipment for Sign Making","active":"1"},
{"id":"22","parent_id":"0","agrpcount":"0","disp_order":"110","code":"application-tape","name":"Application Tapes","active":"1"}
]

每个数组项的容器类

public class Category {
public int id;
public int parent_id;
public int agrpcount;
public int disp_order;
public String code;
public String name;
public int active;
}

加载类别数组的类

public static List<Category> getCategories(){
Category[] cats;

Gson gson = new Gson();

cats = gson.fromJson(strJSON, Category[].class);

List<Category> CatsList = Arrays.asList(cats);
return CatsList;
}

当我运行这段代码时,我在日志中得到以下内容

02-28 15:15:42.471: ERROR/AndroidRuntime(777): Caused by: java.lang.RuntimeException: No-args constructor for class com.signwarehouse.catalog.SWService$Category does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor.java:107)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonDeserializationVisitor.visitChildAsObject(JsonDeserializationVisitor.java:95)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonArrayDeserializationVisitor.visitArray(JsonArrayDeserializationVisitor.java:72)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:117)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.Gson.fromJson(Gson.java:568)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.Gson.fromJson(Gson.java:515)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.Gson.fromJson(Gson.java:484)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.Gson.fromJson(Gson.java:434)
02-28 15:15:42.471: ERROR/AndroidRuntime(777): at com.google.gson.Gson.fromJson(Gson.java:406)

我尝试向 Category 类添加一个无参数构造函数,但这没有帮助。不足为奇,因为 Java 会自动创建它。

有人知道如何解决这个问题吗?

谢谢。

最佳答案

尝试将类别类设为公共(public)静态类...

public static class Category {/* fields */}

我敢打赌,由于您没有将类设为静态,所以它首先尝试创建您的父类/外部类 (com.signwarehouse.catalog.SWService),这可能没有无参数构造函数。

关于java - Android 和 GSON 抛出 No-Args Constructor not found 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5148011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com