java - Serializable subclass of non-serializable parent class -
i hitting brickwall serialization of subclass of location in android/java
location not serializable. have first subclass called falocation not have instance variables. have declared serializable.
then have second class called waypoint looks this:
public class waypoint extends falocation implements serializable { /** * */ private static final long serialversionuid = 1l; /* class variables *******************************************************/ private static int class_version=1; //used 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 match 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(); } }
serialization works fine.
deseriamization produces followwing exception (the leg object contains waypoint).:
10-05 13:50:35.259: warn/system.err(7867): java.io.invalidclassexception: android.location.location; illegalaccessexception 10-05 13:50:35.267: warn/system.err(7867): @ java.io.objectinputstream.resolveconstructorclass(objectinputstream.java:2010) 10-05 13:50:35.267: warn/system.err(7867): @ java.io.objectinputstream.readnewobject(objectinputstream.java:2095) 10-05 13:50:35.267: warn/system.err(7867): @ java.io.objectinputstream.readnonprimitivecontent(objectinputstream.java:929) 10-05 13:50:35.267: warn/system.err(7867): @ java.io.objectinputstream.readobject(objectinputstream.java:2285) 10-05 13:50:35.278: warn/system.err(7867): @ java.io.objectinputstream.readobject(objectinputstream.java:2240) 10-05 13:50:35.278: warn/system.err(7867): @ com.droidfa.navigation.leg.readobject(leg.java:262) .../...
is absolutely necessary serialize location? maybe mark transient, , obtain dynamically after deserializing object. (anyway, documentation ) :
q: if class not implement serializable subclass b implements serializable, fields of class serialized when b serialized?
a: fields of serializable objects written out , restored. object may restored if has no-arg constructor initialize fields of non-serializable supertypes. if subclass has access state of superclass can implement writeobject , readobject save , restore state.
so, if subclass has access fields of non-serializable superclass(es) can use writeobject , readobject protocol implement serialization. otherwise, there fields won't possible serialize.
Comments
Post a Comment