【问题标题】:Sending a bundle to other process将捆绑包发送到其他进程
【发布时间】:2012-05-04 23:41:00
【问题描述】:

我正在使用以下代码序列化对象并通过 putSerializable 将其附加到包,然后通过消息将包发送到其他进程。问题是我收到对象不可序列化的错误。我尝试添加“implements 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;
}

}

【问题讨论】:

  • @Simon : 你能用你准备捆绑和实现 Serialazable 的代码编辑你的帖子吗
  • 添加了调用代码
  • 你确定在你的类中实现了 Serialazable
  • 可序列化的类增加了问题。注意第一行,“扩展可序列化”

标签: android bundle serializable


【解决方案1】:

Bundle 已经是 Parcelable。您可以将Bundle 作为一个值放在另一个Bundle 中,而不必乱用ObjectOutputStream。

【讨论】:

  • 我也可以放一个 ArrayList 的 budles 吗?
猜你喜欢
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2016-03-21
  • 2016-08-09
相关资源
最近更新 更多