Skip to main content

Problem unmarshalling parcelables


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 .


Source: Tips4all

Comments

  1. 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.

    ReplyDelete
  2. 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());

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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();
    }

    ReplyDelete
  5. 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);

    ReplyDelete
  6. 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex