0

I'm trying to serialize my HashMap class and send it over to another intent.

Unfortunatley, i'm getting an "androidruntime-error-parcel-unable-to-marshal-value" exception. How am I suppose to implement serializable on that class ?

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);
    }   
}

Putting the value in the intent:

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; 
        } 
      } 

}
ohadinho
  • 6,894
  • 16
  • 71
  • 124

1 Answers1

-1

Try to put HashMap without serialize it. You don't need to serialize object that you have to pass between intents.

  • 1
    You cannot send an object directly with an Intent... You need either to serialize it, or use Parcelable (which is faster) as explained here : http://stackoverflow.com/questions/2139134/how-to-send-an-object-from-one-android-activity-to-another-using-intents – Dalmas Nov 26 '11 at 13:05
  • Thanks for your both comments. I want TeamsHashMap class to be serialized but I really don't know how to do that – ohadinho Nov 26 '11 at 13:49