gpt4 book ai didi

android - '类的无参数构造函数不存在',但它确实存在

转载 作者:搜寻专家 更新时间:2023-11-01 08:31:47 31 4
gpt4 key购买 nike

我收到错误 类 MwVolleyApi$Page 的无参数构造函数不存在。为这个类型注册一个 Gson 的 InstanceCreator 来解决这个问题。我已经阅读了这篇文章并找到了两种类型的解决方案,但它们似乎都不适合我:

  1. https://github.com/google/gson/issues/135 - 使用静态嵌套类(我用过)
  2. 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 不能实例化私有(private)嵌套类。公开您的 Page 类:

public static class Page {...}

关于android - '类的无参数构造函数不存在',但它确实存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39928588/

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