我正在尝试列出一个都是抽象类的对象列表,但每个对象都有自己的类。此列表需要持久,因此我认为由于过去这样做以来,我就可以实现包裹。只有不同类别的所有抽象类。
我尝试仅制作抽象类包裹,但它不能拥有我习惯的创造者,因为(当然(您无法创建它的实例(因为它是抽象的(。我注意到我注意到人们说您不需要抽象类中的构造函数,而只是在子类中。
AbstractFocuspower类
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();
}
样品子类
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];
}
};
}
我使用的代码
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();
}
不幸的是,这给了我一个我不知道如何修复的错误。
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.
编辑:不确定为什么该帖子是重复的。对于初学者来说,它没有公认的答案。答案需要第三方库。这个问题不是关于单个摘要下的多个子类。
您是否尝试注册类型适配器,例如使用GSON和Abstrack类?我总是为特定格式添加适配器(用于日期,大小数,通常需要非常特定格式的任何内容(,但也用于子类别。
在这种情况下,不需要适配器,这是..直接上的?
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;
}
}
,然后是imph(是的,添加了toString((,hashcode((和equals((我喜欢它们在域对象中的方式..(:
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();
}
}
}
,然后我可以运行以下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);
}
,而gson的初始化就像
private static final Gson GSON = (new GsonBuilder()).registerTypeAdapter(Boolean.class, new JsonBooleanDeAndSerializer()).create();
和我使用的布尔值的类型适配器与您的问题无关。
这很简单,也适合您吗?
检查您的导入。您可能在Pojo中错误地进口了错误的班级。即我已经导入android.net.TransportInfo
,而不是我自己的TransportInfo
类