Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Serializable Exception on custom class - Android

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.

like image 808
pat Avatar asked Jan 17 '23 01:01

pat


1 Answers

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
}
like image 57
wsanville Avatar answered Jan 28 '23 06:01

wsanville