我正在使用以下代码序列化对象并通过 putSerializable 将其附加到包,然后通过消息将包发送到其他进程。问题是我收到对象不可序列化的错误。我尝试添加“实现 Serialazable”,但我仍然得到同样的错误。
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;
}
}
这是进行调用的代码:
ArrayList<byte[]> blist=null;
Bundle b = new Bundle();
if (TriggerList != null && TriggerList.size() > 0)
{
Iterator iter = TriggerList.iterator();
while (iter.hasNext())
{
Bundle entry = (Bundle) iter.next();
if (msg.arg1 == entry.getInt(ProjDefs.APP_ID))
{
if (blist == null)
blist=new ArrayList<byte[]>();
SerBundle sb = new SerBundle(entry);
byte[] bb = serializeObject(sb);
blist.add(bb);
}
}
b.putSerializable(ProjDefs.SERIAL_DATA, blist);
}
NotifyClient(msg.arg1, ProjDefs.GET_APP_TRIGGERS_RESPONSE, 0, 0, b, null);
可序列化的类:
公共类 SerBundle 实现 Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Bundle bundle;
public SerBundle(Bundle bundle)
{
this.bundle = bundle;
}
}