【问题标题】:Sending custom HashMap class from intent to another intent将自定义 HashMap 类从意图发送到另一个意图
【发布时间】:2011-11-26 12:37:27
【问题描述】:

我正在尝试序列化我的 HashMap 类并将其发送到另一个意图。

不幸的是,我遇到了“androidruntime-error-parcel-unable-to-marshal-value”异常。 我应该如何在该类上实现可序列化?

TeamsHashMap.java:

package android.test;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class TeamsHashMap implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private HashMap<String,Team> teamsHashMap;

    public TeamsHashMap()
    {   
        teamsHashMap = new HashMap<String,Team>();      
    }

    public Team GetTeam(String teamName)
    {
        Team team = null;           
        Iterator<Entry<String, Team>> iterator = teamsHashMap.entrySet().iterator();        

        while(team == null && iterator.hasNext())
        {
            Map.Entry<String,Team> pair = (Map.Entry<String,Team>)iterator.next();
            String currentKeyTeamName = pair.getKey();          
            team = teamName.contains(currentKeyTeamName) ? (Team)pair.getValue() : null;
        }

        return team;
    }

    public void AddTeam(String teamName,Team team)
    {
        teamsHashMap.put(teamName, team);
    }   
}

将价值置于意图中:

byte[] teamsHashMapSerialized = SerializerClass.serializeObject(teamsHashMap);
        notificationIntent.putExtra("teamsHashMap", teamsHashMapSerialized);

SerializerClass.java:

package android.infra;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class SerializerClass {
    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) { 

          return null; 
        } 
      }

    public static Object deserializeObject(byte[] b) { 
        try { 
          ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b)); 
          Object object = in.readObject(); 
          in.close(); 

          return object; 
        } catch(ClassNotFoundException cnfe) {        

          return null; 
        } catch(IOException ioe) { 

          return null; 
        } 
      } 

}

【问题讨论】:

    标签: android android-layout android-intent android-activity


    【解决方案1】:

    尽量把HashMap不序列化。您不需要序列化必须在意图之间传递的对象。

    【讨论】:

    • 您不能直接使用 Intent 发送对象...您需要序列化它,或者使用 Parcelable(更快),如下所述:stackoverflow.com/questions/2139134/…
    • 感谢两位 cmets。我希望 TeamsHashMap 类被序列化,但我真的不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    相关资源
    最近更新 更多