1
votes

I am trying to convert a List of a custom object called 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;
    }

}

I am trying to convert it as I read in the Gson documentation:

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

Then an Exception is thrown, here is the logcat output:

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

I looked up for it in google for hours but not of the answers i found helped me, i am not doing anything special in my code, just copied the lines from the official documentation and replaced it with my object.

1
Please post a sample JSON structure that you are having there. This will help figuring out if you have constructed the MyData class correctly. - Reaz Murshed
And moreover, I am not seeing that you are putting something in your appsList variable which needs to be converted into a JSON string. - Reaz Murshed

1 Answers

0
votes

Gson treats Drawable like a regular POJO class and iterates over all getters and tries to serialise them. It looks like Drawable creates cycle with some other objects. Instead of storing whole object, try to serialise only required fields by copying them in constructor:

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
}

or write custom serialiser for Drawable class: