I am using the following code to serialize an object and to attach it to a bundle via putSerializable, then sending the bundle to other process via a Message. The problem is that I get an error that the object is not serializable. i tried adding “implements Serialazable” but I still get the same error.
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;
}
}
This is the code that make the call:
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);
The serializable class:
public class SerBundle implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Bundle bundle;
public SerBundle(Bundle bundle)
{
this.bundle = bundle;
}
}
A
Bundleis alreadyParcelable. You can put aBundleas a value inside of anotherBundle, without having to mess around withObjectOutputStream.