我正在尝试制作一个将小部件(表示为视图)存储在数据库中的 Android 应用程序,然后能够在以后重新创建它们。
这是它的要点:
public class DBSerializer<T extends View & Serializable> {
public int storeWidget(T widget) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(widget);
byte[] data = bytes.toByteArray();
bytes.close();
out.close();
<store "data" in database>;
return <unique database id>;
}
public T getWidget(int id) {
byte[] data = <get data from database for the given id>;
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
return (T)in.readObject();
}
}
不幸的是,in.readObject()
抛出 InvalidClassException 和 message android.view.View; IllegalAccessException
。
既然Android的View直接继承了Object,而且Object有一个无参构造函数,这不应该行吗?或者它是否试图使用无参数构造函数调用 View?错误消息对于异常的确切原因不是很清楚。
有谁知道是什么原因造成的以及如何解决?