4

So I'm trying to pass an instance of a class I create by intent to a new activity.

public class Room implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 6857044522819206055L;


    int roomID;

    String roomName;

    ArrayList<MarkerHolder> markerHolders = new ArrayList<MarkerHolder>();


    public int getRoomID() {
        return roomID;
    }
    public void setRoomID(int roomID) {
        this.roomID = roomID;
    }

    public String getRoomName() {
        return roomName;
    }
    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }       

    public ArrayList<MarkerHolder> getMarkerHolders() {
        return markerHolders;
    }
    public void setMarkerHolders(ArrayList<MarkerHolder> markerHolders) {
        this.markerHolders = markerHolders;
    }       
}

public class MarkerHolder implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = -7334724625702415322L;
    String marker;
    String markerTag;
    public String getMarker() {
        return marker;
    }
    public void setMarker(String marker) {
        this.marker = marker;
    }
    public String getMarkerTag() {
        return markerTag;
    }
    public void setMarkerTag(String markerTag) {
        this.markerTag = markerTag;
    }
}

And I try to pass that class by

Intent svc = new Intent(this, RoomUploader.class);
svc.putExtra("room", room);

try{           
    startService(svc);  
}catch (Exception e){
     e.printStackTrace();
}

and I keep getting a Not Serializable Exception which I can't figure out. Both classes implement serializable and have serial Ids. The member variables are just strings, ints, and an array of another class that is also serializable that contains only strings. As far as I know all these things should be serializable, what else could cause this error? Thanks in advance.

pat
  • 1,005
  • 4
  • 12
  • 29

3 Answers3

9

Are those classes inner classes of your activity or another class? If so, they have a reference to their outer class (which may or may not be serializable), and you can solve this by making those classes static.

Example:

public static class Room implements Serializable
{
    //your implementation
}

public static class MarkerHolder implements Serializable
{
    //your implementation
}
wsanville
  • 37,158
  • 8
  • 76
  • 101
0

Try changing ArrayList to a native array of MarkerHolder:

MarkerHolder[] markerHolders;

Update: My bad. I've always used native array for serialization so not aware ArrayList is indeed serialzable.

Your code looks right. What was the exact error message printed in the logcat (i.e. which class threw the serialization exception)?

Another solution (more work) is to make your objects implement the Parcable interface.

azgolfer
  • 15,087
  • 4
  • 49
  • 46
  • 1
    Actually, `ArrayList` *is* serializable: http://developer.android.com/reference/java/util/ArrayList.html – wsanville Jul 19 '12 at 21:47
  • hmm yea and I need to use arrayList because I need to dynamically add elements (you can't do that to array in Java right?) Anything else that could be wrong with the code above? – pat Jul 19 '12 at 21:49
0

Try to use getApplicationContext() or context instaed of this.

Husam A. Al-ahmadi
  • 2,056
  • 2
  • 20
  • 27