Gson错误:java.lang.IllegalArgumentException:类android.app.ExitTransitionCoordinator声明了多个名为mHandler的JSON字段

时间:2019-04-01 20:25:46

标签: android json serialization gson

我正在尝试转换名为List的自定义对象的MyData

class MyData {
    private String id;
    private String title;
    private Drawable AppIcon;

    public MyData() {}

    public MyData(String id, String title, Drawable _AppIcon) {
        this.id = id;
        this.title = title;
        this.AppIcon = _AppIcon;
    }


    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public Drawable getAppIcon() {
        return this.AppIcon;
    }

}

我正在尝试按照Gson文档中的说明进行转换:

Type listType = new TypeToken<List<MyData>>(){}.getType();
List<MyData> appsList = new ArrayList<>();
String ArrayStr = new Gson().toJson(appsList, listType);

然后抛出一个Exception,这是logcat的输出:

java.lang.IllegalArgumentException: class android.app.ExitTransitionCoordinator declares multiple JSON fields named mHandler
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:170)
        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.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.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.ArrayTypeAdapter$1.create(ArrayTypeAdapter.java:48)
        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.ReflectiveType

我花了好几个小时在google中寻找它,但没有找到能帮助我的答案,我的代码没有做任何特别的事情,只是复制了官方文档中的代码行,并用我的对象替换了。

1 个答案:

答案 0 :(得分:0)

GsonDrawable视为常规POJO类,并遍历所有getters并尝试对其进行序列化。看起来Drawable与其他一些对象一起创建了循环。与其存储整个对象,不如尝试通过将它们复制到构造函数中来仅序列化必填字段:

public MyData(String id, String title, Drawable appIcon) {
    this.id = id;
    this.title = title;
    this.minWidth = appIcon.getMinimumWidth();
    this.direction = appIcon.getLayoutDirection();
    // copy other fields
}

或为Drawable类编写自定义序列化程序: