61

I've got a few classes that implement Parcelable and some of these classes contain each other as properties. I'm marshalling the classes into a Parcel to pass them between activities. Marshalling them TO the Parcel works fine, but when I try to unmarshall them I get the following error:

...
AndroidRuntime  E  Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: schemas.Arrivals.LocationType
AndroidRuntime  E   at android.os.Parcel.readParcelable(Parcel.java:1822)
AndroidRuntime  E   at schemas.Arrivals.LayoverType.<init>(LayoverType.java:121)
AndroidRuntime  E   at schemas.Arrivals.LayoverType.<init>(LayoverType.java:120)
AndroidRuntime  E   at schemas.Arrivals.LayoverType$1.createFromParcel(LayoverType.java:112)
AndroidRuntime  E   at schemas.Arrivals.LayoverType$1.createFromParcel(LayoverType.java:1)
AndroidRuntime  E   at android.os.Parcel.readTypedList(Parcel.java:1509)
AndroidRuntime  E   at schemas.Arrivals.BlockPositionType.<init>(BlockPositionType.java:244)
AndroidRuntime  E   at schemas.Arrivals.BlockPositionType.<init>(BlockPositionType.java:242)
AndroidRuntime  E   at schemas.Arrivals.BlockPositionType$1.createFromParcel(BlockPositionType.java:234)
AndroidRuntime  E   at schemas.Arrivals.BlockPositionType$1.createFromParcel(BlockPositionType.java:1)
...

The LayoverType class (where it's failing):

public class LayoverType implements Parcelable {    
    protected LocationType location;
    protected long start;
    protected long end;

    public LayoverType() {}

    public LocationType getLocation() {
        return location;
    }

    public void setLocation(LocationType value) {
        this.location = value;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long value) {
        this.start = value;
    }

    public long getEnd() {
        return end;
    }

    public void setEnd(long value) {
        this.end = value;
    }


    // **********************************************
    //  for implementing Parcelable
    // **********************************************

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(location, flags);
        dest.writeLong(start);
        dest.writeLong(end  );
    }

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

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

    private LayoverType(Parcel dest) {
        location = (LocationType) dest.readParcelable(null);    // it's failing here
        start = dest.readLong();
        end   = dest.readLong();
    }
}

Here's the LocationType class:

public class LocationType implements Parcelable {
    protected int locid;
    protected String desc;
    protected String dir;
    protected double lat;
    protected double lng;

    public LocationType() {}

    public int getLocid() {
        return locid;
    }

    public void setLocid(int value) {
        this.locid = value;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String value) {
        this.desc = value;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String value) {
        this.dir = value;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double value) {
        this.lat = value;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double value) {
        this.lng = value;
    }

    // **********************************************
    //  for implementing Parcelable
    // **********************************************


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt   (locid);
        dest.writeString(desc );
        dest.writeString(dir  );
        dest.writeDouble(lat  );
        dest.writeDouble(lng  );
    }

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

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

    private LocationType(Parcel dest) {
        locid = dest.readInt   ();
        desc  = dest.readString();
        dir   = dest.readString();
        lat   = dest.readDouble();
        lng   = dest.readDouble();
    }
}

Update 2: As far as I can tell it's failing at the following bit of code (from Parcel's source):

Class c = loader == null ? Class.forName(name) : Class.forName(name, true, loader);

Why is it not able to find the class? It both exists and implements Parcelable.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • Could you post the code for the LocationType class? It could be the write method in that class is not correct in some way. – mkamowski Jan 03 '10 at 21:21
  • @MpKamowski: I added the Parcelable bits from LocationType. The only other stuff in either class is just getters/setters. – Jeremy Logan Jan 03 '10 at 23:05
  • Is it possible that the package for which LocationType is a apart of is not visible in the code where it is being unmarshalled? – mkamowski Jan 04 '10 at 13:55
  • I can't see how. `LocationType` and `LayoverType` are in the same package. I went ahead and tried implicitly importing `LocationType` into `LayoverType` and it made no difference. – Jeremy Logan Jan 04 '10 at 18:28
  • Could you post the full code for both of your classes? I'm especially interested in LocationType to see whether it's a public static inner class or not. – Romain Guy Feb 09 '11 at 22:50
  • I went ahead and included everything but the package line and the imports. Hope that helps and thanks for taking a look at this. – Jeremy Logan Feb 09 '11 at 22:50
  • 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

10 Answers10

83

Because this was not answered in "answer" but in comment I will post an answer: As @Max-Gontar pointed you should use LocationType.class.getClassLoader() to get the correct ClassLoader and get rid of ClassNotFound exception, i.e.:

in.readParceleable(LocationType.class.getClassLoader());

Ognyan
  • 13,452
  • 5
  • 64
  • 82
  • 1
    This was so helpful! In my case, I had to pass in the class loader when unmarshalling a Parcelable object that contained a bundle that also contained instances of the same type of object. Ex. public class MyObject implements Parcelable, Serializable { protected Bundle mBundle; MyObject(Parcel parcel) { mBundle = parcel.readBundle(MyObject.class.getClassLoader()); } } – Ben Jakuben Mar 30 '12 at 21:55
36

I had the same problem with the following setup: some handler creates a Message and sends its over a Messenger to a remote service.

the Message contains a Bundle where I put my Parcelable descendant:

final Message msg = Message.obtain(null, 0);
msg.getData().putParcelable("DOWNLOADFILEURLITEM", downloadFileURLItem);

messenger.send(msg);

I had the same exception when the remote service tried to unparcel. In my case, I had overseen that the remote service is indeed a separate os process. Therefore, I had to set the current classloader to be used by the unparcelling process on the service side:

final Bundle bundle = msg.getData();
bundle.setClassLoader(getClassLoader());

DownloadFileURLItem urlItem = (DownloadFileURLItem)
bundle.getParcelable("DOWNLOADFILEURLITEM");

Bundle.setClassLoader sets the classloader which is used to load the appropriate Parcelable classes. In a remote service, you need to reset it to the current class loader.

Andre Steingress
  • 4,381
  • 28
  • 28
9

I found the problem was I was not passing my applications ClassLoader to the unmarshalling function:

in.readParceleable(getContext().getClassLoader());

Rather than:

in.readParceleable(null); 

OR

in.readParceleable(MyClass.class.getClassLoader()); 
sud007
  • 5,824
  • 4
  • 56
  • 63
mxcl
  • 26,392
  • 12
  • 99
  • 98
  • 15
    I suppose you mean LocationType.class.getClassLoader() – Maksym Gontar Jun 03 '10 at 06:04
  • That looked hopeful, but does not solve my problem. Can you please post the minimal project that illustrates the problem and its solution? I am struggling with the sample code from Google and it crashes in the receiver-activity. – mobibob Aug 17 '10 at 04:17
  • 1
    @MaxGontar yes probably. But it's so long since I wrote this I don't want to edit it in case it is wrong. – mxcl Dec 13 '11 at 22:20
  • 1
    I used `MyApplication.class.getClassLoader()` as we were using generics for a parcelable object, worked fine. – Chris.Jenkins Sep 05 '12 at 15:37
  • @MaxHowell i got the same issue, I made my own Parcelable object which has a HashMap to save the columnsNames and its values. My Parcelable Object save all the data in the Parce object in the `writeToParcelMethod(Parcel dest, int flags)` and retrieve them in the readFromParcel(Parcel parcel)` method. after i put all Parcelable objects inside of `ArrayList` and i send it(from a remote process) to an activity by `Messenger` object. even if i do `data.setClassLoader(getClassLoader())` but when i try to populate the parcelable objects from the list they are null. thk – AXSM Mar 15 '13 at 01:01
8

Just adding my 2 cents here, because I lost more than half a day scratching my head on this. You might get this error if you don't put the writes methods and reads methods in the exact same order. For instance the following would be wrong:

 @Override
    // Order: locid -> desc -> lat -> dir -> lng
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt   (locid);
        dest.writeString(desc);
        dest.writeDouble(lat);
        dest.writeString(dir);
        dest.writeDouble(lng);
    }
    // Order: locid -> desc -> dir -> lat -> lng
    private LocationType(Parcel dest) {
        locid = dest.readInt();
        desc  = dest.readString();
        dir   = dest.readString();
        lat   = dest.readDouble();
        lng   = dest.readDouble();
    }

By the way the author did this correctly but it might help someone one day.

Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
6

I got ClassNotFoundException too and posting my solution bc the answers here led me to the right direction. My scenario is that I have nested parcelable objects. Object A contains an ArrayList of Object's B. Both implement Parcelable. Writing the list of B Object's in class A:

@Override
public void writeToParcel(Parcel dest, int flags) {
    ...
    dest.writeList(getMyArrayList());

}

Reading the list in class A:

public ObjectA(Parcel source) {
    ...
    myArrayList= new ArrayList<B>();
    source.readList(myArrayList, B.class.getClassLoader());
}

Thank you!

Camino2007
  • 796
  • 10
  • 17
6

I am not very familiar with Parcelable but if it's anything like Serialization each call to write an object that implements the interface will cause a recursive call to writeToParcel(). Therefore, if something along the call stack fails or writes a null value the class that initiated the call may not be constructed correctly.

Try: Trace the writeToParcel() call stack through all the classes starting at the first call to writeToParcel() and verify that all the values are getting sent correctly.

mkamowski
  • 334
  • 2
  • 7
  • I was definately having problems with NullPointers before, but I initialized all the values and that went away. Now it seems like it just can't find the class. – Jeremy Logan Jan 03 '10 at 23:06
  • Oh, it appears you were right all along. As best I can tell it was crapping out because something wasn't getting initialized and when it tried to read the next `Parcellable` out it was getting a null (or something). – Jeremy Logan Jan 05 '10 at 18:55
  • can you tell us what exactly you fixed? I'm having the same problem and don't see what's wrong with my code. – mxk Aug 24 '10 at 13:42
  • 24
    I also fixed it, here's what I learned from my own mistakes (and the atrocious documentation from Binder/Parcel/Parcelable): 1) you have to pair calls to write/read something *exactly* (i.e. you cannot use writeValue() to write a `Parcelable` and readParcelable() to read it). 2) Don't use readTypedList(), it always broke on me. Instead, createTypedArrayList() is much eaiser to use and also worked as expected. 3) on *every* call to readXYZ where it expects a `ClassLoader`, pass that class' loader, not null. That's also for readBundle() when it must unparcel values in its internal map. HTH. – mxk Aug 25 '10 at 13:51
  • Is there any chance that someone knows of a working implementation example of this? – icecream Mar 23 '11 at 15:07
  • using ClassLoader instead of null inside readList() did the trick! thanks! – a'' May 03 '13 at 14:43
5

Instead of using writeParcelable and readParcelable use writeToParcel and createFromParcel directly. So the better code is:

@Override
public void writeToParcel(Parcel dest, int flags) {
    location.writeToParcel(dest, flags);
    dest.writeLong(start);
    dest.writeLong(end  );
}

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

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

private LayoverType(Parcel dest) {
    location = LocationType.CREATOR.createFromParcel(dest);
    start = dest.readLong();
    end   = dest.readLong();
}
tannewt
  • 81
  • 1
  • 2
2

Well I had the same problem and solve it in a very silly way that I dont know if its called a solution at all.

lets say you have this class you want to pass to another activity

public class Person implements  Parcelable,Serializable{
public  String Name;
public  int Age;

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

 dest.writeString(name);
dest.writeInt(age);

}

public SeriesInfo(Parcel in) {

age= in.readInt(); //her was my problem as I have put age befor name
//while in the writeToParcel function I have defined
//dest.writeInt(age) after  in.readString();???!!!!
name= in.readString();

}
}

Thats it When I changed the:

dest.writeString(name);
dest.writeInt(age);

to

dest.writeInt(age);
dest.writeString(name);

The problem was solved???!!!!

Ahmad Moussa
  • 1,296
  • 18
  • 22
0

If you have an Object with a property of type of List objects you should pass the class loader when you read the property for example:

public class Mall implements Parcelable {

    public List<Outstanding> getOutstanding() {
        return outstanding;
    }

    public void setOutstanding(List<Outstanding> outstanding) {
        this.outstanding = outstanding;
    }        

    protected Mall(Parcel in) {
        outstanding = new ArrayList<Outstanding>();
        //this is the key, pass the class loader
        in.readList(outstanding, Outstanding.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(outstanding);
    }

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

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

Note: Is important that the class Outstanding implements the Parceable interface.

klefevre
  • 8,595
  • 7
  • 42
  • 71
shontauro
  • 1,812
  • 1
  • 25
  • 26
0

I got this exception because I was missing a constructor. The same must be done for all classes that implement Parcelable:

// add new constructor
@RequiresApi(Build.VERSION_CODES.N)
private LocationType(Parcel dest, ClassLoader loader) {
    super(dest, loader);
    locid = dest.readInt();
    desc  = dest.readString();
    dir   = dest.readString();
    lat   = dest.readDouble();
    lng   = dest.readDouble();
}

public static final Creator<LayoverType> CREATOR = new ClassLoaderCreator<LayoverType>() {

    // add createFromParcel method with ClassLoader
    @Override
    public LayoverType createFromParcel(Parcel in, ClassLoader loader)
    {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? new LayoverType(in, loader) : new LayoverType(in);
    }

    public LayoverType createFromParcel(Parcel in) {
        // call other createFromParcel method.    
        return createFromParcel(in, null);
    }

    public LayoverType[] newArray(int size) {
        return new LayoverType[size];
    }
};
Wirling
  • 4,810
  • 3
  • 48
  • 78