“类的无参数构造函数不存在”,但它确实存在

2023-12-21

我收到错误No-args constructor for class MwVolleyApi$Page does not exist. Register an InstanceCreator with Gson for this type to fix this problem。我已经阅读了此内容并找到了两种类型的解决方案,这两种解决方案似乎都不适合我:

  1. https://github.com/google/gson/issues/135 https://github.com/google/gson/issues/135- 使用静态嵌套类(我做了)
  2. 类 XXX 的无参数构造函数不存在 https://stackoverflow.com/questions/29548983/no-args-constructor-for-class-xxx-does-not-exist- 为类创建一个无参数构造函数(我做了)

我缺少什么?

完整错误消息:

10-08 17:07:53.790: E/Volley(31902): [4551] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: No-args constructor for class fr.free.nrw.commons.upload.MwVolleyApi$Page does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
10-08 17:07:53.790: E/Volley(31902): java.lang.RuntimeException: No-args constructor for class fr.free.nrw.commons.upload.MwVolleyApi$Page does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:41)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:101)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor.java:107)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChildAsObject(JsonDeserializationVisitor.java:95)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonArrayDeserializationVisitor.visitArray(JsonArrayDeserializationVisitor.java:73)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor.java:107)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChildAsArray(JsonDeserializationVisitor.java:102)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonObjectDeserializationVisitor.visitArrayField(JsonObjectDeserializationVisitor.java:81)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:154)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChild(JsonDeserializationVisitor.java:107)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationVisitor.visitChildAsObject(JsonDeserializationVisitor.java:95)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonObjectDeserializationVisitor.visitObjectField(JsonObjectDeserializationVisitor.java:62)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.navigateClassFields(ObjectNavigator.java:156)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:123)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.Gson.fromJson(Gson.java:495)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.Gson.fromJson(Gson.java:444)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.Gson.fromJson(Gson.java:396)
10-08 17:07:53.790: E/Volley(31902):    at com.google.gson.Gson.fromJson(Gson.java:372)
10-08 17:07:53.790: E/Volley(31902):    at fr.free.nrw.commons.upload.MwVolleyApi$QueryRequest.parseNetworkResponse(MwVolleyApi.java:140)
10-08 17:07:53.790: E/Volley(31902):    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:123)
10-08 17:07:53.790: E/fr.free.nrw.commons.upload.MwVolleyApi$LogResponseErrorListener(31902): com.android.volley.VolleyError: java.lang.RuntimeException: No-args constructor for class fr.free.nrw.commons.upload.MwVolleyApi$Page does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

相关代码:

private static class QueryRequest extends JsonRequest<QueryResponse> {
    private static final String TAG = QueryRequest.class.getName();

    public QueryRequest(String url,
                        Response.Listener<QueryResponse> listener,
                        Response.ErrorListener errorListener) {
        super(Request.Method.GET, url, null, listener, errorListener);
    }

    @Override
    protected Response<QueryResponse> parseNetworkResponse(NetworkResponse response) {
        String json = parseString(response);
        QueryResponse queryResponse = GSON.fromJson(json, QueryResponse.class);
        return Response.success(queryResponse, cacheEntry(response));
    }

    private Cache.Entry cacheEntry(NetworkResponse response) {
        return HttpHeaderParser.parseCacheHeaders(response);
    }

    private String parseString(NetworkResponse response) {
        try {
            return new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            return new String(response.data);
        }
    }
}


private static class QueryResponse {
    private Query query = new Query();

    private String printSet() {
        if (categorySet == null || categorySet.isEmpty()) {
            GpsCatExists.setGpsCatExists(false);
            Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists());
            return "No collection of categories";
        } else {
            GpsCatExists.setGpsCatExists(true);
            Log.d(TAG, "gpsCatExists=" + GpsCatExists.getGpsCatExists());
            return "CATEGORIES FOUND" + categorySet.toString();
        }

    }

    @Override
    public String toString() {
        if (query != null) {
            return "query=" + query.toString() + "\n" + printSet();
        } else {
            return "No pages found";
        }
    }
}

private static class Query {
    private Page [] pages;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("pages=" + "\n");
        if (pages != null) {
            for (Page page : pages) {
                builder.append(page.toString());
                builder.append("\n");
            }
            builder.replace(builder.length() - 1, builder.length(), "");
            return builder.toString();
        } else {
            return "No pages found";
        }
    }
}

private static class Page {
    private int pageid;
    private int ns;
    private String title;
    private Category[] categories;
    private Category category;

    public Page() {
    }

    @Override
    public String toString() {

        StringBuilder builder = new StringBuilder("PAGEID=" + pageid + " ns=" + ns + " title=" + title + "\n" + " CATEGORIES= ");

        if (categories == null || categories.length == 0) {
            builder.append("no categories exist\n");
        } else {
            for (Category category : categories) {
                builder.append(category.toString());
                builder.append("\n");
                if (category != null) {
                    String categoryString = category.toString().replace("Category:", "");
                    categorySet.add(categoryString);
                }
            }
        }

        categoryList = new ArrayList<String>(categorySet);
        builder.replace(builder.length() - 1, builder.length(), "");
        return builder.toString();
    }
}

private static class Category {
    private String title;

    @Override
    public String toString() {
        return title;
    }
}

Gson 无法实例化私有嵌套类。做你的Page公共类:

public static class Page {...}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

“类的无参数构造函数不存在”,但它确实存在 的相关文章

随机推荐

  • matplotlib中如何限制y轴高度?

    如何限制matplotlib图中y轴的高度 我正在尝试显示 x 轴 并降低该一维图的图形高度 我尝试过设置刻度 图形大小 tight layout 边距等 但没有成功 另外 无论我选择什么限制 更改 ylimit 都会跨越整个图形高度 im
  • Maven 故障安全插件不运行并行测试

    我有一个 Maven POM 文件 当我提供并行执行选项时 我在日志中没有看到任何并行执行的迹象 XML 调试让我抓狂 有什么想法这里出了什么问题吗
  • 查找两条曲线之间的重叠面积

    我一直在努力寻找解决方案来找到两条曲线之间的重叠区域 我处理的不是具有已知参数的概率密度函数 而是通过平滑经验数据点获得的曲线 我发现的唯一提示是计算不重叠的区域 如这段代码 来自here https www researchgate ne
  • 使用 python 求解 7000x7000 线性系统时的最佳性能方法

    我需要一种有效的方法来反转 python 中的 7000x7000 空气动力学影响系数 密集 矩阵 在使用 FORTRAN 例程之前 我已经开始使用 LAPACK 中的 LU 分解例程来处理问题 我已经看到它在其他相关应用程序中的使用非常有
  • 用户输入数字时的 Javascript 计算器

    我是 Javascript 新手 但我正在尝试在我的网站上实现一些功能 用户可以在其中输入数量 并且小计会在输入时动态更新 例如 如果每件物品的价格为 10 美元 并且用户在文本字段中键入 5 我希望它在文本框旁边显示 50 美元 非常简单
  • REST - 使用 Spring MVC 返回创建的对象

    我有一个 REST 调用 它接受一个 JSON 对象 比如说一个人 创建此对象 经过验证并保存到数据库 后 我需要返回新创建的 JSON 对象 我认为标准做法是返回201 Accepted而不是立即返回对象 但我的应用程序立即需要新创建的对
  • 有人使用 Sphinx 来记录 C++ 项目吗? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Sinatra/Ruby 默认一个参数

    有没有办法在 Sinatra 中默认参数 我目前正在寻找是否 start 作为参数传递 但看起来有点hacky 如果我能告诉 Sinatra 在未指定的情况下默认某些参数 那就太好了 get comments do want to setu
  • 选择 html 元素内的文本并更改样式

    text1 div text2 div 我只需要选择 text1 并添加一些样式 I tried body color red 但text1和text2都变成红色 我正在寻找类似的东西 css selector im searching c
  • 使用新的 prefixText 时如何更改 TextInputLayout 的提示填充?

    我尝试过实施TextInputLayout与新的prefixText using com google android material material 1 2 0 alpha02 这是一个非常酷的功能 但是当我添加前缀文本时 提示标签会
  • 为什么 C/C++ 编译器需要在编译时知道数组的大小?

    我知道 C99 以及 C 之前的 C 标准规定 堆栈上数组的大小必须在编译时已知 但这是为什么呢 堆栈上的数组是在运行时分配的 那么为什么大小在编译时很重要呢 希望有人向我解释编译器在编译时将如何处理大小 谢谢 这种数组的示例是 void
  • 具有最小宽度的 3 列布局(固定、流动、固定)

    我在网上搜索过 似乎找不到一个干净 简单 所有浏览器友好的三栏布局 我希望有 3 列布局 左列固定为 200px 右列固定为 200px 中间列保留剩余宽度 但最小宽度为 600px 所以整体最小宽度是 200px 600px 200px
  • 如何访问angular2组件中的全局js变量

    我在下面定义了一个全局 js 变量 Url 是一个 ASP Net MVC html 帮助器 它将转换为字符串值 如何访问 angular2 组件中的 rootVar 我曾经在 Angular 1 5 中使用窗口服务 在 Angular2
  • 使用 ADO.Net 实体模型的优点和缺点

    HI 使用 ADO NET 实体模型作为数据层有哪些优缺点 如果我要使用这项技术 我应该使用 LINQ 吗 Thanks 首先 您不必使用 LINQ 来使用实体框架 EF 但它确实有帮助 EF 基于 EF 团队所称的东西实体SQL 因此 当
  • Linq 合并左连接数据

    假设我有以下数据库 Users UserId PK UserName Roles RoleId PK RoleName UserRoles UserId PK RoleId PK 用户 1 M 用户角色 M 1 角色 使用 LinqToSQ
  • 如何在 Windows 7 GAC 中注册 Assembly .net 4.0?

    您好 我正在使用 vs2010 创建一个 net 4 0 程序集 我假装在 GAC 上注册该程序集 我找不到 gacutil 也不知道如何在 4 0 GAC 上注册程序集 有人可以帮忙吗 找到解决方案 使用位于以下位置的 GacUtil 进
  • 如何按行长度对文件进行排序,然后按字母顺序对第二个键进行排序?

    假设我有一个文件 ab aa c aaaa 我希望它像这样排序 c aa ab aaaa 即按行长度排序 然后按字母顺序排序 这在 bash 中可能吗 您可以在每行前面添加行的长度 然后进行数字排序 最后剪掉数字 lt your file
  • PostgreSQL - 使 ts_rank 按原样采用 ts_vector 位置或定义自定义 ts_rank 函数

    我正在对电子商务平台中的一系列商品执行加权搜索 我遇到的问题是 ts rank 为不同的单词组合提供了完全相同的值 即使 ts vector 为每组单词提供了不同的位置 让我用一个例子来说明这一点 如果我给 ts vector 这个词cam
  • zsh 和 bash 的 echo 有什么区别?

    在 bash 中 在这种特定情况下 echo 的行为如下 bash c echo a nb a nb 但在 zsh 中 同样的事情结果却截然不同 zsh c echo a nb a b 和鱼 因为我很好奇 fish c echo a nb
  • “类的无参数构造函数不存在”,但它确实存在

    我收到错误No args constructor for class MwVolleyApi Page does not exist Register an InstanceCreator with Gson for this type t