我在 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();
}
}
序列化工作正常。
反序列化产生followwing异常(腿对象包含一个路点)。:
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): 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)
.../...
最佳答案
是否绝对有必要序列化 Location?也许您可以将其标记为 transient ,并在反序列化对象后动态获取它。 (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.
因此,如果子类可以访问其不可序列化父类(super class)的字段,则它可以使用 writeObject 和 readObject 协议(protocol)来实现序列化。否则会出现无法序列化的字段。
关于java - 不可序列化父类的可序列化子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7662579/