2012-04-24 45 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

相關問題