2012-04-24 86 views
0

我使用下面的代码来序列化一个对象,并通过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; 
} 

}

+0

@Simon:u能与其中u正准备捆绑,并实现Serialazable – 2012-04-24 10:36:23

+0

代码编辑您的帖子补充说打的电话 – Simon 2012-04-24 10:50:27

+0

是U确保您 – 2012-04-24 10:53:58

回答

0

Bundle已经是Parcelable。您可以将Bundle作为值存入另一个Bundle,而不必与ObjectOutputStream混淆。

+0

我也可以把一个ArrayList的budles? – Simon 2012-04-24 11:44:44

+0

@Simon:是的,使用['putParcelableArrayList()'](http://goo.gl/5tpcg) – CommonsWare 2012-04-24 11:46:09

相关问题