9

I'm pretty new to Serializable and Parcelable. I'm having a hard time passing an instance of this object from an application to a remote service:

public class Event implements Parcelable, Cloneable {

    /** Defines under what Bundle's key architecture events are stored */ 
    public static final String BUNDLE_KEY = "ZKEvent";

    /** Defines which messages are architecture's events */
    public static final int MSG_WHAT = 0xDEFECABE;

    /** Defines a key to store map under a Bundle to put into a Parcel (oh yeah!) */
    private static final String MAP_KEY = "MAP";

    /** Indicates this event ID. This ID should be the same in the event answer if any */
    private int idEvent = 0;

    /** Indicates this event priority */
    private int priority = EventPriority.EVENT_PRIORITY_LOW;

    /** ID for this event's sender */
    private int idSource = 0;

    /** ID for this event's destination */
    private int idDestination = 0;

    /** Action to be performed */
    private int action = EventAction.DEFAULT;

    public static final Parcelable.Creator<Event> CREATOR =
        new Parcelable.Creator<Event>() {
            public Event createFromParcel(Parcel in) {
            return new Event(in);
        }

        public Event[] newArray(int size) {
            return new Event[size];
        }
    };

    /** 
     * Event's data
     * Represented as 
     *      key -> parameter;
     *      value -> data 
     */
    private Map<String, Object> data = new HashMap<String, Object>();

    /** Default constructor */
    public Event() {};

    /** Contructor with ID of the event */
    public Event(int id) {
        idEvent = id;
    }

    /** Constructor for Parcelable */
    public Event(Parcel in) {
        readFromParcel(in);
    }

    /** Gets event's priority */
    public int getPriority() {
        return priority;
    }

    /** Sets this event's priority */
    public void setPriority(int priority) {
        this.priority = priority;
    }

    /** Gets the ID of this event's sender */   
    public int getIdSource() {
        return idSource;
    }

    /** Sets the ID of this event's sender */
    public void setIdSource(int idSource) {
        this.idSource = idSource;
    }

    /** Gets the ID of this event's destination */
    public Integer getIdDestination() {
        return idDestination;
    }

    /** Sets the ID of this event's destination */
    public void setIdDestination(Integer idDestination) {
        this.idDestination = idDestination;
    }

    /** Gets the action of this event */
    public int getAction() {
        return action;
    }

    /** Sets the action of this event */
    public void setAction(int action) {
        this.action = action;
    }

    /** Gets the data of this event */
    public Object getData(String key) {
        return data.get(key);
    }

    /** Sets the data of this event  */
    public void addData(String key, Object data) {
        this.data.put(key, data);
    }

    /** Gets the ID of this event */
    public int getIdEvent() {
        return idEvent;
    }

    /** Sets the ID of this event */
    public void setIdEvent(int idEvent) {
        this.idEvent = idEvent;
    }

    @Override
    public Object clone() {

        Event clone = new Event();

        clone.action = this.action;
        clone.idDestination = this.idDestination;
        clone.idEvent = this.idEvent;
        clone.idSource = this.idSource;
        clone.priority = this.priority;

        for(Entry<String,Object> entry: this.data.entrySet()) {
            clone.data.put(entry.getKey(), entry.getValue());
        }

        return clone;
    }

    @Override
    public int describeContents() {
        // TODO Nothing here
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {

        out.writeInt(idEvent);
        out.writeInt(priority);
        out.writeInt(idSource);
        out.writeInt(idDestination);
        out.writeInt(action);

        Bundle mapBundle = new Bundle();
        mapBundle.putSerializable(MAP_KEY, (Serializable) data);
    out.writeBundle(mapBundle);
    }

    /** Restoring this object from a Parcel */
    public void readFromParcel(Parcel in) {

        idEvent = in.readInt();
        priority = in.readInt();
        idSource = in.readInt();
        idDestination = in.readInt();
        action = in.readInt();

        Bundle mapBundle = in.readBundle();
        data = (Map<String, Object>) mapBundle.getSerializable(MAP_KEY);
    }
}

So I create a new instance of this Event class and pass it to the service like this:

/** Register with service */
private void registerWithService() {
    Event registerEvent = EventFactory.getEvent();
    registerEvent.setAction(EventAction.REGISTER);
    registerEvent.setIdSource(1);

    Bundle data = new Bundle();
    data.putParcelable(Event.BUNDLE_KEY, registerEvent);

    Message msg = new Message();
    msg.setData(data); // Exception thrown here in the Service
    msg.what = Event.MSG_WHAT;
    try {
        outMessenger.send(msg);
    } catch (RemoteException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

And here where in the remote service the exception is thrown:

    public void handleMessage(Message msg) {
        if (msg.what == Event.MSG_WHAT) {
            // Get the data bundle
            Bundle bundle = msg.getData();
            // Extract the event from the message
            Event event = (Event) bundle.get(Event.BUNDLE_KEY); // Exception thrown here
            sendEvent(event);
        } else {
            Log.i(TAG, "Received non architecture message, dropping...");
        }
    }

And the stacktrace:

07-09 12:38:10.947: E/AndroidRuntime(2234): FATAL EXCEPTION: main
07-09 12:38:10.947: E/AndroidRuntime(2234): android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.blabla.android.core.event.Event
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Parcel.readParcelable(Parcel.java:1958)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Parcel.readValue(Parcel.java:1846)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Parcel.readMapInternal(Parcel.java:2083)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Bundle.unparcel(Bundle.java:208)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Bundle.get(Bundle.java:260)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at com.blabla.android.core.event.EventManager$EventHandler.handleMessage(EventManager.java:44)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.os.Looper.loop(Looper.java:123)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at android.app.ActivityThread.main(ActivityThread.java:3683)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at java.lang.reflect.Method.invoke(Method.java:507)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-09 12:38:10.947: E/AndroidRuntime(2234):     at dalvik.system.NativeStart.main(Native Method)

Any help greatly appreciated, thanks!

m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • http://stackoverflow.com/questions/1996294/problem-unmarshalling-parcelables/3806769#3806769 – Akram Jul 09 '12 at 11:26
  • Is your service running in the same process as the creator of the Bundle you're sending to it, or is this an interprocess request? – Jules Jul 09 '12 at 11:27
  • @Akki: I read that answer, but I don't know what to do with that `ClassLoader` later... Also why would I need a separate ClassLoader if that class is included in both applications? @Jules: it's a *remote* service, so it's an IPC. – m0skit0 Jul 09 '12 at 11:59
  • possible duplicate of ["BadParcelableException: ClassNotFoundException when unmarshalling " while using Parcel.read method that has a ClassLoader as argument](http://stackoverflow.com/questions/18126249/badparcelableexception-classnotfoundexception-when-unmarshalling-myclass-wh) – Flow Aug 30 '14 at 13:15
  • @Flow You mean the other question is a duplicate of this one... – m0skit0 Sep 02 '14 at 20:34

2 Answers2

16

In this method, you never write the Bundle to the Parcel:

public void writeToParcel(Parcel out, int flags) {

    out.writeInt(idEvent);
    out.writeInt(priority);
    out.writeInt(idSource);
    out.writeInt(idDestination);
    out.writeInt(action);

    Bundle mapBundle = new Bundle();
    mapBundle.putSerializable(MAP_KEY, (Serializable) data);
   // You need to actually write the Bundle to the Parcel here...
}

EDIT Added additional solution to the ClassLoader problem:

Set the classloader in handleMessage() in your remote service like this:

Bundle bundle = msg.getData();
bundle.setClassLoader(Event.class.getClassLoader());
Event event = (Event) bundle.get(Event.BUNDLE_KEY);
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I've added it to the Parcel, still the same exception. Edited the question. – m0skit0 Jul 09 '12 at 14:01
  • Is the exception exactly the same? In the same place, with the same error? Please post the exception again. – David Wasser Jul 09 '12 at 14:18
  • The exception is **exactly** the same. – m0skit0 Jul 09 '12 at 14:37
  • 1
    Try this: Set the classloader in `handleMessage()` in your remote service like this: `Bundle bundle = msg.getData(); bundle.setClassLoader(Event.class.getClassLoader()); Event event = (Event) bundle.get(Event.BUNDLE_KEY);` – David Wasser Jul 09 '12 at 16:08
  • Yes that was it, thank you! If you include this in the answer, I will accept it. – m0skit0 Jul 10 '12 at 07:57
  • Done. Glad to be of assistance! – David Wasser Jul 10 '12 at 08:20
  • Do you know why why I have to set the class loader there? – m0skit0 Jul 12 '12 at 20:51
  • Normally you don't need to much around with ClassLoaders. Please try something. Remove the calls to `setClassLoader()` **everywhere**. It could be that you are creating the problem by setting the ClassLoader. – David Wasser Jul 13 '12 at 07:59
  • I only have `setClassLoader()` where you told me to put it. I edited the code to reflect latest one. – m0skit0 Jul 13 '12 at 08:07
  • In the code you posted in the question, you set it also in `registerWithService()`. Have you removed that too? – David Wasser Jul 13 '12 at 08:08
  • Yes, I removed that one after posting this question in fact. – m0skit0 Jul 13 '12 at 08:09
  • 1
    OK, well I don't really know why you need to set the ClassLoader in this case. Obviously the one that is set as default isn't able to see your classes (It is probably the System ClassLoader so it can only see the standard Android stuff). I'll do a bit more research and see if I can come up with something. Interesting problem... – David Wasser Jul 13 '12 at 08:11
  • Yes, you were correct, it's Android ClassLoader because the thread doing the unparcel is a system thread. – m0skit0 Dec 24 '13 at 22:02
  • 4
    Upvote for `getClassLoader()`. It helped me solve the `BadParcelableException` by changing this: `parcel.readSparseArray(null);` to this: `parcel.readSparseArray(MyClass.class.getClassLoader());` – kris larson Feb 19 '16 at 14:56
  • 1
    Using Serializable is not a solution for not getting Parcable working it is just a workaround. Parcable is much faster than Serializable that is the reason tha Parcable exists!! – Roel Mar 09 '16 at 12:06
  • 1
    @Roel You are missing the point. There is nothing in this whole discussion about using `Serializable` instead of `Parcelable`. If you were to actually look at the original post, you sill see that OP is using `Parcelable` and was having issues using that, due to some silly mistakes and also due to class loader issues. – David Wasser Mar 09 '16 at 18:29
0

Only solution found so far:

 val bundle = Bundle().apply {
        classLoader = YourClassName::class.java.classLoader
        putParcelable("deepLinkData", YourClassName(param1, param2, param3))
 }

please note, deepLinkData is a key, can be anything

    val myIntent = Intent(Intent.ACTION_VIEW)
    myIntent.putExtra("deepLinkBundle", bundle)
pinkfloyd
  • 255
  • 4
  • 13