我需要序列化一个 GeoPoints 列表,然后将它们读回。我在网上搜索,发现我可以实现自己的 GeoPoint 并实现可序列化。它可以工作,我可以将对象写入 .ser 文件,但是当我读回它时,我得到一个 invalidclassexception,它继续说详细消息 - 原始 GeoPoint 对象的非法访问异常。我假设我没有在 MyGeoPoint 类中正确扩展 GeoPoint。有人可以看看并告诉我我在哪里犯了错误吗?
import com.google.android.maps.GeoPoint;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class MyGeoPoint extends GeoPoint implements Serializable {
static final long serialVersionUID = -3010695769693014199L;
public MyGeoPoint(int latitude, int longitude){
super(latitude, longitude);
}
public MyGeoPoint(){
this(0,0);
}
private void writeObject(ObjectOutputStream s) throws IOException{
s.defaultWriteObject();
}
private void readObject(ObjectInputStream s)
throws java.io.IOException, java.lang.ClassNotFoundException{
s.defaultReadObject();
}
}
然后它就像
File filex = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator +"floodData.ser");
MyGeoPoint aPoint = new MyGeoPoint();
MyGeoPoint readPoint = null;
//write
try{
FileOutputStream f = new FileOutputStream(filex);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(aPoint);
s.close();
} catch (IOException e) {
e.printStackTrace();
}
//read
try {
FileInputStream ff = new FileInputStream(filex);
ObjectInputStream ss = new ObjectInputStream(ff);
readPoint = (MyGeoPoint)ss.readObject();
ss.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
//end serialisationtest