Not Serializable Exception for custom class - Android

So, I am trying to pass an instance of the class that I am creating, by intent for a new action.

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'm trying to pass this class

 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 cannot understand. Both classes implement serializable and have sequential identifiers. Member variables are just strings, ints, and an array of another class that is also serialized, containing only strings. As far as I know, all these things should be serializable, what else could cause this error? Thanks in advance.

+4
source share
3 answers

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

Example:

 public static class Room implements Serializable { //your implementation } public static class MarkerHolder implements Serializable { //your implementation } 
+9
source

Try changing ArrayList to your own MarkerHolder array:

 MarkerHolder[] markerHolders; 

Update: my bad. I always used my own array for serialization, so I don’t know that ArrayList is really serializable.

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

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

0
source

Try using getApplicationContext () or context .

0
source

All Articles