pat
pat

Reputation: 1015

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.

Upvotes: 4

Views: 2805

Answers (3)

Husam A. Al-ahmadi
Husam A. Al-ahmadi

Reputation: 2056

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

Upvotes: 0

azgolfer
azgolfer

Reputation: 15137

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.

Upvotes: 0

wsanville
wsanville

Reputation: 37516

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
}

Upvotes: 9

Related Questions