How to fix: Unable to invoke no-args constructor for class X: Registering an InstanceCreator with Gson for this type may fix this problem

I am trying to make a list of objects that are all of an abstract class, but each are there own class. This list needs to persistent so I figured I implement parcelable since I have done so in the past. Only not with different classes all of an abstract class.

I tried just making the abstract class parcelable but that can't have a creator that I am used to because (of course) you can't create an instance of it (because it is abstract). Reading around I noticed that people said you dont need a constructor in the abstract class, just in the subclasses.

AbstractFocusPower class

public abstract class AbstractFocusPower implements Parcelable {

    private transient AppExtension app;
    private ImplementSchool school;
    private String name;
    private int duration;
    private int cost;
    private int altCost;
    private int requiredLevel;
    private boolean isSelected;
    private boolean isResonant;
    private int nofSpirtBonusUsed;

    /**
     * Constructor for Focus Power with no alternative cost
     */
    public AbstractFocusPower(AppExtension app, ImplementSchool school, String name, int requiredLevel, int duration, int cost, boolean isSelected) {
        this.app = app;
        this.school = school;
        this.name = name;
        this.requiredLevel = requiredLevel;
        this.duration = duration;
        this.cost = cost;
        this.altCost = -1;
        this.isSelected = isSelected;
        this.isResonant = false;
    }

    // I cut out the other constructors

    public abstract AbstractFocusPower makeCopy();

    public abstract String getDescription();

    // I cut out the getters and setters

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.school == null ? -1 : this.school.ordinal());
        dest.writeString(this.name);
        dest.writeInt(this.duration);
        dest.writeInt(this.cost);
        dest.writeInt(this.altCost);
        dest.writeInt(this.requiredLevel);
        dest.writeByte(this.isSelected ? (byte) 1 : (byte) 0);
        dest.writeByte(this.isResonant ? (byte) 1 : (byte) 0);
        dest.writeInt(this.nofSpirtBonusUsed);
    }

    protected AbstractFocusPower(Parcel in) {
        int tmpSchool = in.readInt();
        this.school = tmpSchool == -1 ? null : ImplementSchool.values()[tmpSchool];
        this.name = in.readString();
        this.duration = in.readInt();
        this.cost = in.readInt();
        this.altCost = in.readInt();
        this.requiredLevel = in.readInt();
        this.isSelected = in.readByte() != 0;
        this.isResonant = in.readByte() != 0;
        this.nofSpirtBonusUsed = in.readInt();
    }

Sample subclass

public class AegisFocusPower extends AbstractFocusPower {

    public AegisFocusPower(AppExtension app) {
        super(app, ImplementSchool.ABJURATION, app.getString(R.string.focus_power_name_aegis), 0, 1, 1, false);
    }

    @Override
    public String getDescription() {
        return getApp().getString(R.string.focus_power_desc_aegis, (1+((int) Math.floor(getApp().getCurrentCharacter().getOccultistLevel()/6.0))));
    }

    @Override
    public AegisFocusPower makeCopy() {
        return new AegisFocusPower(getApp());
    }

    public AegisFocusPower(Parcel in) {
        super(in);
    }

    public static final Parcelable.Creator<AegisFocusPower> CREATOR = new Parcelable.Creator<AegisFocusPower>() {
        public AegisFocusPower createFromParcel(Parcel in) {
            return new AegisFocusPower (in);
        }

        public AegisFocusPower [] newArray(int size) {
            return new AegisFocusPower[size];
        }
    };
}

Code where I use it

Gson gsonFocusPowers = new Gson();
        String jsonFocusPowers = sharedPreferences.getString(FOCUS_POWERS_GSON, null);
        Type typeFocusPower = new TypeToken<ArrayList<AbstractFocusPower>>() {
        }.getType();
        ArrayList<AbstractFocusPower> focusPowers;
        focusPowers = gsonFocusPowers.fromJson(jsonFocusPowers, typeFocusPower);
        if (focusPowers != null) {
            this.focusPowers.addAll(checkForNewFocusPowers(focusPowers));
        } else {
            this.focusPowers = getNewFocusPowerList();
        }

Unfortunately this gives me an error which I don't know how to fix.

java.lang.RuntimeException: Unable to create application nl.rekijan.occultistmentalfocushelper.AppExtension: java.lang.RuntimeException: Unable to invoke no-args constructor for class nl.rekijan.occultistmentalfocushelper.mvc.focuspowers.AbstractFocusPower. Registering an InstanceCreator with Gson for this type may fix this problem.

Edit: Not sure why that post is a duplicate. For starters it doesn't have an accepted answer. The answer requires a 3rd party library. The question isn't about multiple subclasses under a single abstract.


Solution 1:

have you tried registering a type adapter, something like Using Gson and Abstract Classes ? I always add adapters both for specific formatting (for dates, big decimals, anything where you usually require a very specific format) but also for sub-classing.

In this case however, no adapter is needed, this is.. straight on?

public abstract class AbstractFocusPower implements Parcelable {

    // just some property needed to be pushed through a constructor
    protected final String myString;

    protected AbstractFocusPower(String myString) {
        this.myString = myString;
    }
}

and then the impl (yeah added toString(), hashCode() and equals() the way I like them to be in domain objects..):

public class AegisFocusPower extends AbstractFocusPower {

    boolean imParcelled;

    public AegisFocusPower(String myString) {
        super(myString);
    }

    @Override //yup the interface impl
    public void parcelMe() {
        imParcelled = true;
    }

    @Override
    public String toString() {
        return new StringBuilder("{ imParcelled : ").append(imParcelled).append(", myString : ").append(myString).append(" }").toString();
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) {
            return true;
        } else if (other == null || !(other instanceof AegisFocusPower)) {
            return false;
        } else {
            return other.hashCode() == hashCode();
        }
    }
}

and then I can run the following junit :

@Test
public void AegisFocusPowerToJsonAndBack(){

    // single instance
    AegisFocusPower ea = new AegisFocusPower("apa");
    String json = GSON.toJson(ea);
    assertEquals("{\"imParcelled\":\"false\",\"myString\":\"apa\"}", json);
    AegisFocusPower backAtYa = (AegisFocusPower) GSON.fromJson(json, AegisFocusPower.class);
    assertEquals(backAtYa, ea);

    // A list
    AegisFocusPower ea2 = new AegisFocusPower("bepa");
    AegisFocusPower ea3 = new AegisFocusPower("cepa");
    List<AegisFocusPower> powerList = new ArrayList<>();
    powerList.add(ea2);
    powerList.add(ea3);
    String jsonList = GSON.toJson(powerList);
    assertEquals("[{\"imParcelled\":\"false\",\"myString\":\"bepa\"},{\"imParcelled\":\"false\",\"myString\":\"cepa\"}]", jsonList);
    List<AegisFocusPower> backAtYaz = Arrays.asList(GSON.fromJson(jsonList,AegisFocusPower[].class));
    assertEquals(backAtYaz.get(0), ea2);
    assertEquals(backAtYaz.get(1), ea3);
}

whereas GSON is initialized simply like

private static final Gson GSON = (new GsonBuilder()).registerTypeAdapter(Boolean.class, new JsonBooleanDeAndSerializer()).create();

and the type adapter registered for booleans which I use is irrelevant for your problem.

This is.. simple enough and would work for you too?