我有一个表布局,它实现了可保留表行的可序列化。在表格行中,它将包含一个编辑文本和一个复选框,确切地说,很像一个 list 。
但是,当我尝试序列化表格布局(CheckList)时,它告诉我表格行不可序列化。之后我也将其实现为可序列化的。但它告诉我 EditText 和 CheckBox 不可序列化。
我该如何解决这个问题?
更新:(Serializer.java)
public class Serializer {
public static byte[] serializeObject(Object o) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(o);
out.close();
// Get the bytes of the serialized object
byte[] buf = bos.toByteArray();
return buf;
} catch(IOException ioe) {
Log.e("serializeObject", "error", ioe);
return null;
}
}
public static Object deserializeObject(byte[] b) {
try {
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
Object object = in.readObject();
in.close();
return object;
} catch(ClassNotFoundException cnfe) {
Log.e("deserializeObject", "class not found error", cnfe);
return null;
} catch(IOException ioe) {
Log.e("deserializeObject", "io error", ioe);
return null;
}
}
}
这就是我序列化 list 的方式:
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putSerializable("checklist", Serializer.serializeObject(checklist));
intent.putExtra("checklist", bundle);
setResult(Activity.RESULT_OK, intent);
finish();
这是我尝试反序列化它的方法:
try {
Bundle extras = data.getExtras();
Bundle checklist_bundle = extras.getBundle("checklist");
byte[] bytes = (byte[]) checklist_bundle.getSerializable("checklist");
CheckList checklist = (CheckList) Serializer.deserializeObject(bytes);
} catch (Exception e) {
e.printStackTrace();
}
最佳答案
我将扩展该类,使扩展的类可序列化,然后使用它。
不确定我还可以添加什么来使其更加充实,但我想我们可以随着时间的推移在它的基础上继续发展? :)
关于java - Java 对象序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327016/