
我正在使用android / java中的Location子类序列化打砖块
位置不可序列化.
我有一个名为FALocation的第一个子类,它没有任何实例变量.我已声明它可序列化.
然后我有一个名为Waypoint的第二个类看起来像这样:
public class Waypoint extends FALocation implements Serializable { /** * */ private static final long serialVersionUID = 1L; /* Class variables *******************************************************/ private static int CLASS_VERSION=1; //Used to version parcels /* Instance variables ****************************************************/ private transIEnt String type=DataHelper.PT_TYPE_US; private transIEnt String country; private transIEnt String name=null; private transIEnt String description=null; private transIEnt int elevation = 0; private transIEnt int population = 0; // Afterthought, added to match the DB structure /* Constructors **********************************************************/ public Waypoint() { super(); } public Waypoint(double lat, double lon, String name, String description) { super(lat, lon); this.setname(name); this.setDescription(description); } public Waypoint(Location l) { super(l); } public Waypoint(String provIDer) { super(provIDer); } /* Implementing serializable */ private voID writeObject(java.io.ObjectOutputStream out) throws IOException { Log.v("DroIDFA", "Serialising \"%s\" (v%d).", Waypoint.class.getSimplename(), CLASS_VERSION); out.writeInt(CLASS_VERSION); out.writeObject(type); out.writeObject(country); out.writeObject(name); out.writeObject(description); out.writeInt(elevation); out.writeInt(population); } private voID readobject(java.io.ObjectinputStream in) throws IOException, ClassNotFoundException { int serialClassversion = in.readInt(); Log.v("DroIDFA", "Deserialising \"%s\" (v%d).", Waypoint.class.getSimplename(),serialClassversion); type = (String) in.readobject(); country = (String) in.readobject(); name = (String) in.readobject(); description = (String) in.readobject(); elevation = in.readInt(); population = in.readInt(); }}序列化工作正常.
Deseriamization产生跟随异常(leg对象包含一个航点):
10-05 13:50:35.259: WARN/System.err(7867): java.io.InvalIDClassException: androID.location.Location; illegalaccessexception10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectinputStream.resolveConstructorClass(ObjectinputStream.java:2010)10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectinputStream.readNewObject(ObjectinputStream.java:2095)10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectinputStream.readNonPrimitiveContent(ObjectinputStream.java:929)10-05 13:50:35.267: WARN/System.err(7867): at java.io.ObjectinputStream.readobject(ObjectinputStream.java:2285)10-05 13:50:35.278: WARN/System.err(7867): at java.io.ObjectinputStream.readobject(ObjectinputStream.java:2240)10-05 13:50:35.278: WARN/System.err(7867): at com.droIDfa.navigation.Leg.readobject(Leg.java:262).../...解决方法:
是否绝对有必要序列化位置?也许你可以将它标记为瞬态,并在反序列化对象后动态获取它. (Anyway, from the documentation ):
Q: If class A does not implement Serializable but a subclass B implements Serializable, will the fIElds of class A be serialized when B is serialized?
A: Only the fIElds of Serializable objects are written out and restored. The object may be restored only if it has a no-arg constructor that will initialize the fIElds of non-serializable supertypes. If the subclass has access to the state of the superclass it can implement writeObject and readobject to save and restore that state.
因此,如果子类可以访问其非可序列化超类的字段,则可以使用writeObject和readobject协议来实现序列化.否则,将存在无法序列化的字段.
总结以上是内存溢出为你收集整理的java – 非可序列化父类的Serializable子类全部内容,希望文章能够帮你解决java – 非可序列化父类的Serializable子类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)