我有一个单独的对象,它有三个字段:两个字符串和一个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未初始化时,一切都正常工作。提前感谢您的帮助。
发布于 2011-02-02 23:01:00
java.io.NotSerializableException: android.graphics.drawable.BitmapDrawable
这条消息看起来非常清楚-- photo
字段中的特定可绘制实例是一个BitmapDrawable,它不是被设计成序列化的。如果不处理不可序列化字段,则无法序列化您的类。
如果您可以确保您的类始终具有BitmapDrawable
或Bitmap,您可以查看以下代码以获取如何处理Bitmap
字段的示例:
发布于 2011-02-02 23:01:41
你不能序列化它。
简单地说,如果BitmapDrawable是不可序列化的,那么您就不能序列化它。通常这样的东西是不可序列化的,因为它们持有对不是纯数据的东西的引用。类似于绘图图面的上下文或句柄。
https://stackoverflow.com/questions/4880583
复制相似问题