問題我有一個實現Serializable這使tablerows一個tablelayout。在表格中,它將包含一個edittext和一個複選框,非常類似於清單。與序列化對象的Java
然而,當我嘗試序列化tablelayout(表)它告訴我的是,tablerows不serizable。之後我也實現了它可序列化。但是,它告訴我EditText和CheckBox是不可序列化的。
我該如何解決這個問題?
UPDATE:(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;
}
}
}
這是我如何序列化的清單:
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();
}
感謝您的回覆。檢查表擴展了我創建的項目類,而項目類擴展了一個視圖類,你知道我該如何擴展它嗎? –
我還添加了一些代碼,以便如何序列化和反序列化CheckList。希望它會更清晰。 –
你不需要改變任何東西:只要確保所有的類都實現了Serializable'。您不需要創建讀取和寫入方法,除非它以默認方式不適用於您。 (老實說,我從來沒有默認的方法失敗!) – corsiKa