ClassNotFoundException when unmarshalling and really don't know why

20,324

Solution 1

FIX I finally found the error and I just can't freaking belive it!!!

Searched for almost 1 1/2 days and all it needed was switching the last two methods in readFromParcel or writeToParcel because they were not in the exact same order!!

Can't believe it but now it works. For every one who gets the same mistake, check the order of writeToParcel and readFromParcel. Thay have to be in the same order.

(Christian Risch solved it by himself. Still, I believe in neatness on this site. Maybe he'll close the answer himself)

Solution 2

For whoever stumbles upon this problem, the answer above kind of worked for me but not exactly. My problem was that I had a field initializer not in the correct order.

the code was like this:

protected Shop(Parcel in) {
    id = in.readInt();
    contactInfo = in.readParcelable(ShopContactInfo.class.getClassLoader());
    name = in.readString();
}

and in my class the fields were declared in the following order:

@SerializedName("Id")
public int id;

@SerializedName("Name")
public String name;

@SerializedName("ContactInfo")
public ShopContactInfo contactInfo;

notice the

name = in.readString();

is under

contactInfo = in.readParcelable(ShopContactInfo.class.getClassLoader());

the following code fixed it:

protected Shop(Parcel in) {
    id = in.readInt();
    name = in.readString();
    contactInfo = in.readParcelable(ShopContactInfo.class.getClassLoader());
}

Solution 3

@Override public void writeToParcel(Parcel dest, int flags) { // could alternatively bundle first...

    dest.writeString(songs_id);
    dest.writeInt(check_song_favorite);
    dest.writeString(name);
    dest.writeString(track_image);
    dest.writeString(created_date);
    dest.writeString(status);
    dest.writeString(track);
    dest.writeString(artist_name);
    dest.writeString(uploader_name);
    dest.writeString(play_count);
    dest.writeString(share_count);
    dest.writeString(favorite_count);
    dest.writeString(genre);
    dest.writeString(user_id);
    dest.writeString(offline_download);
    dest.writeString(slug);
}

/**
 * Creates ArtistParcelable model from Parcel.
 */
public static final Parcelable.Creator<PlayerData> CREATOR = new Creator<PlayerData>() {
    @Override
    public PlayerData createFromParcel(Parcel source) {
        String songs_id = source.readString();
        int check_song_favorite = source.readInt();
        String name = source.readString();
        String track_image = source.readString();
        String created_date = source.readString();
        String status = source.readString();
        String track = source.readString();
        String artist_name = source.readString();
        String uploader_name = source.readString();
        String play_count = source.readString();
        String share_count = source.readString();
        String favorite_count = source.readString();
        String genre = source.readString();
        String user_id = source.readString();
        String offline_download = source.readString();
        String slug = source.readString();

        return new PlayerData(check_song_favorite,songs_id, name, created_date, track_image, status, track,
                artist_name, uploader_name, play_count, share_count, favorite_count,genre,user_id,offline_download,slug);
    }

    @Override
    public PlayerData[] newArray(int size) {
        return new PlayerData[size];
    }
};

try to manage writeToParcel and readFromParcel in same manner. Solved my issue.

Share:
20,324

Related videos on Youtube

Christian Risch
Author by

Christian Risch

Updated on July 22, 2022

Comments

  • Christian Risch
    Christian Risch almost 2 years

    FIXED: ANSWER AT BOTTOM

    I am writing an android app for a project and im trying to use parcelable objects. I have 2 parcalable classes. The first is a normal class with only 2 arguments. The second one is a class with additional attributes and a list with type from the first class and an additional Object of it.

    Every time i try to pass an object of the second class to another Activity, i get android.os.BadParcelableException: ClassNotFoundException when unmarshalling

    and the app closes.

    I searched for hours now and tried multiple solutions but nothing helped. The Error doesnt come when i only pass an object of the first class to another activity..

    Here my code:

    package de.softwareproject.v3.testclasses;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class Cart implements Parcelable {
    
    private String name;
    private Integer priceInCents;
    
    public Cart() {}
    
    public Cart(Parcel in){
        readFromParcel(in);
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getPriceInCents() {
        return priceInCents;
    }
    public void setPriceInCents(Integer priceInCents) {
        this.priceInCents = priceInCents;
    }
    
    public static final Parcelable.Creator<Cart> CREATOR = new Parcelable.Creator<Cart>() {
    
                @Override
                public Cart createFromParcel(Parcel source) {
                    return new Cart(source);
                }
    
                @Override
                public Cart[] newArray(int size) {
                    return new Cart[size];
                }
    };
    
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(priceInCents);
    }
    
    private void readFromParcel(Parcel in) {
        name = in.readString();
        priceInCents = in.readInt();
    }
    }
    

    My Second Class:

    package de.softwareproject.v3.testclasses;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class CurrentUser implements Parcelable {
    
    private String username;
    private String eMail;
    private String password;
    private List<Cart> carts;
    private Cart activeCart;
    
    public CurrentUser() {}
    
    public CurrentUser(Parcel in) {
        readFromParcel(in);
    }
    
    private void readFromParcel(Parcel in) {
        username = in.readString();
        eMail = in.readString();
        password = in.readString();
        activeCart = in.readParcelable(de.softwareproject.v3.testclasses.Cart.class.getClassLoader());
        if (carts == null){ carts = new ArrayList<Cart>(); }
        in.readTypedList(carts, Cart.CREATOR);
    }
    
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String geteMail() {
        return eMail;
    }
    public void seteMail(String eMail) {
        this.eMail = eMail;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public List<Cart> getCarts() {
        return carts;
    }
    public void setCarts(List<Cart> carts) {
        this.carts = carts;
    }
    public Cart getActiveCart() {
        return activeCart;
    }
    public void setActiveCart(Cart activeCart) {
        this.activeCart = activeCart;
    }
    
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(username);
        dest.writeString(eMail);
        dest.writeString(password);
        dest.writeTypedList(carts);
        dest.writeParcelable(activeCart, flags);
    }
    
    public static final Parcelable.Creator<CurrentUser> CREATOR = new Parcelable.Creator<CurrentUser>() {
    
        @Override
        public CurrentUser createFromParcel(Parcel in) {
            return new CurrentUser(in);
        }
    
        @Override
        public CurrentUser[] newArray(int size) {
            return new CurrentUser[size];
        }
    };
    }
    

    Please help me.

    Edit: Here my Logcat

        03-15 16:24:08.139: E/Parcel(689): Class not found when unmarshalling: ??, e: java.lang.ClassNotFoundException: ??
        03-15 16:24:08.149: D/AndroidRuntime(689): Shutting down VM
        03-15 16:24:08.149: W/dalvikvm(689): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
        03-15 16:24:08.229: E/AndroidRuntime(689): FATAL EXCEPTION: main
        03-15 16:24:08.229: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.softwareproject.v3/de.softwareproject.v3.Startpage}: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ??
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Handler.dispatchMessage(Handler.java:99)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Looper.loop(Looper.java:137)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread.main(ActivityThread.java:4340)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at java.lang.reflect.Method.invokeNative(Native Method)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at java.lang.reflect.Method.invoke(Method.java:511)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at dalvik.system.NativeStart.main(Native Method)
        03-15 16:24:08.229: E/AndroidRuntime(689): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: ??
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Parcel.readParcelable(Parcel.java:1966)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at de.softwareproject.v3.testclasses.CurrentUser.readFromParcel(CurrentUser.java:26)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at de.softwareproject.v3.testclasses.CurrentUser.<init>(CurrentUser.java:19)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at de.softwareproject.v3.testclasses.CurrentUser$1.createFromParcel(CurrentUser.java:80)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at de.softwareproject.v3.testclasses.CurrentUser$1.createFromParcel(CurrentUser.java:1)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Parcel.readParcelable(Parcel.java:1992)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Parcel.readValue(Parcel.java:1854)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Parcel.readMapInternal(Parcel.java:2094)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Bundle.unparcel(Bundle.java:223)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.os.Bundle.getParcelable(Bundle.java:1158)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at de.softwareproject.v3.Startpage.onCreate(Startpage.java:34)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.Activity.performCreate(Activity.java:4465)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
        03-15 16:24:08.229: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
        03-15 16:24:08.229: E/AndroidRuntime(689):  ... 11 more
        03-15 16:24:10.289: I/Process(689): Sending signal. PID: 689 SIG: 9
    

    FIX I finally found the error and I just can't freaking belive it!!!

    Searched for almost 1 1/2 days and all it needed was switching the last two methods in readFromParcel or writeToParcel because they were not in the exact same order!!

    Can't believe it but now it works. For every one who gets the same mistake, check the order of writeToParcel and readFromParcel. Thay have to be in the same order.

    Have a nice day, Christian**

    • DigCamara
      DigCamara over 11 years
      Post your logcat to see exactly where you're getting the exception.
    • Christian Risch
      Christian Risch over 11 years
      i did, hope you can read it
    • MemLeak
      MemLeak over 11 years
      have you proofen the validity of your input?
    • Christian Risch
      Christian Risch over 11 years
      sorry, i don't know what you mean by that. i'm new in android-programming. do you mean how i initialize my classes?
    • DigCamara
      DigCamara over 11 years
      Seems very similar to this question: stackoverflow.com/questions/1996294/…
    • Christian Risch
      Christian Risch over 11 years
      Thanks. I will try this when i get home. I'll write if it helped. :)
    • Christian Risch
      Christian Risch over 11 years
      Found the mistake! Wrote the solution into the question. Thanks for the help :)
    • Flow
      Flow almost 11 years
      Please put your solution as answer and mark the question accepted.
    • Roel
      Roel over 8 years
      offcourse the order has to be good. How else must it now which property is what?
    • Saraschandraa
      Saraschandraa almost 7 years
      Really Man. The answer may be very simple but no one figured out. Salute..
  • sud007
    sud007 over 7 years
    Just died laughing on that one! Hilarious! It was a humour punch on SOF; unexpected!
  • Mohamed Nageh
    Mohamed Nageh over 6 years
    LOL , i didn't see that coming :))
  • me_
    me_ almost 6 years
    I'm glad you believe in neatness... do you believe in not taking 300 points that weren't yours?
  • DigCamara
    DigCamara almost 6 years
    ... you are right. Those points aren't rightfully mine. I believe this way the answer reached more people than it would have otherwise. And, of course, if @christian Risch wants to add it as his own answer, of if a mod wants to re-assign the answer to him, I would not mind it at all.
  • Niroj Shr
    Niroj Shr over 5 years
    Lost my 1 day.. What a small mess sometimes leads greater mess
  • Yogesh Srivastava
    Yogesh Srivastava about 4 years
    You save my time I faced same issue, just change my sequence and now its working fine