问题序列化绘制对象
问题描述:
我有它有三个字段燎对象:两个字符串和Drawable
问题序列化绘制对象
public class MyObject implements Serializable {
private static final long serialVersionUID = 1L;
public String name;
public String lastName;
public Drawable photo;
public MyObject() {
}
public MyObject(String name, String lastName, Drawable photo) {
this.name = name;
this.lastName = lastName;
this.photo = photo;
}
}
我想要做的,就是这些对象的ArrayList
保存到一个文件,但我不断收到一个NotSerializableException
02-02 23:06:10.825: WARN/System.err(13891): java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
,我用它来存储文件中的代码:
public static void saveArrayList(ArrayList<MyObject> arrayList, Context context) {
final File file = new File(context.getCacheDir(), FILE_NAME);
FileOutputStream outputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
outputStream = new FileOutputStream(file);
objectOutputStream = new ObjectOutputStream(outputStream);
objectOutputStream.writeObject(arrayList);
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
if(objectOutputStream != null) {
objectOutputStream.close();
}
if(outputStream != null) {
outputStream.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
当drawable未初始化时,一切正常。 在此先感谢您的帮助。
答
java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
此消息似乎非常清楚 - 在photo
领域的具体绘制实例是BitmapDrawable,这是不是要被序列化。如果不处理不可序列化的字段,则无法序列化您的类。
如果你能保证你的类将永远有一个BitmapDrawable
或Bitmap,你可以看到这个代码如何处理Bitmap
领域的一个例子:
答
你不能序列化。
简而言之,如果BitmapDrawable不是Serializable,那么你就不能序列化它。通常这样的事情是不可序列化的,因为它们持有对非纯数据事物的引用。像上下文或绘图表面的句柄一样。
感谢您的回答!这是我一直在寻找的解决方案。 – srgtuszy 2011-02-02 23:11:32