0

NOTE: the solution is in the answer starting with SOLUTION that I can't accept since it's my own anwer. I hope this helps someone else.

I want to make my class, that includes a parcelable object, parcelable. In other words let's say I have a class with 3 fields: String name, int id and Location loc. Now I want to make this class Parcelable. How can I do it? So far that's what I did:

public MyClass(Parcel in) {
name = in.readString();
id = in.readInt();
loc = in.readParcelable(Location.class.getClassLoader()); }

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(id);
dest.writeParcelable(loc, flags); }

When I go to use it I get and error: Class not found when unmarshalling... Does anyone know why I get this error and how to fix it? Thanks in advance for any answer.

EDIT:

I have to add that since I'm sending this data to a service working on his own process I'm using the Handler technique so maybe that could be the problem. But still that method is done well since it was working perfectly until I introduce the Location object in MyClass.

user2294708
  • 65
  • 1
  • 9
  • I even tried to follow what's written in the following link but it still gives me the same error: http://stackoverflow.com/questions/8112565/how-to-put-a-location-object-in-parcelable – user2294708 Jul 22 '14 at 10:17

2 Answers2

1

SOLUTION: I found the solution. My implementation was fine. The problem was that I send the message at a separate process so I need to set the proper classLoader in order to make it work right. So the upper code is correct and the thing I had to change was when I go to retrive the object from the bundle or the message. I'll paste the link that gave me the solution even if nobody set it as the correct answer: Problem unmarshalling parcelables

Community
  • 1
  • 1
user2294708
  • 65
  • 1
  • 9
0

This part of the code seems right.

  • Make sure that inside Location you do the Parcel stuff correctly as well.
  • One other thing that could go wrong is, that if you serialized the class before you had the Location in it and now you are trying to de-serialize the old object which doesn't have Location yet.
Luka Bradeško
  • 542
  • 7
  • 16
  • Thanks for the answer. About the 2 points you were talking about, in the first one I actually use Location of android.location.Location so I assume it's correct. Fo the second point I think I don't get it. Could you explain it a bit more plz. – user2294708 Jul 22 '14 at 09:49
  • Another thing that could be useful to point out is that I've tried to pass the MyClass object by passing all the single elements instead of the instance of the class itself and it did work. In other words, if I do like that: Bundle b = new Bundle(); b.putString("testName", name); b.putInt("testId", id); b.putParcelable("testLoc", loc); and then I send it and I retrive it in the other class it works. If I do send like b.putParcelable("test", myClass); where myClass in an instance of MyClass I get the error – user2294708 Jul 22 '14 at 09:54
  • don't mind 2. Its only relevant if you write the parcel to storage and the try to read it later when the class itself could already change. I didn't know you use android's Location class. – Luka Bradeško Jul 22 '14 at 11:01