Alex
Alex

Reputation: 1345

ClassNotFoundException when unmarshalling a Parcelable

I have a problem with parsing a Parcelable across an Intent.

I create parcelable using

Intent data = new Intent();
data.putExtra(ShoppingListAdapter.parcelName, la);
setResult(Activity.RESULT_OK, data);

I receive it in the onActivityResult:

Parcelable myData = data.getParcelableExtra(ShoppingListAdapter.parcelName);

Then pass it to another Activity using:

Intent myIntent = new Intent(this,Class.class);
myIntent.putExtra("myData", myData);
startActivityForResult(myIntent, RESULT);

My Parcelable has another Parcelable inside which I write and read uisng:

list = in.readParcelable(null);

I have tried using different class loaders, from ClassLoader.getSystemLoader() to MyClass.class.getClassLoader() but still I get a Runtime Exception:

06-12 21:13:04.940: ERROR/AndroidRuntime(29962): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: 

Is my Parcel corrupted somewhere before this or am I reading it wrong?

Alex

Upvotes: 0

Views: 4970

Answers (3)

user2081479
user2081479

Reputation: 21

I had that problem too, it's a silly thing. The order and number of the methods on writeToParcel and readToParcel should be the same and with the same variables.

I mean:

public void writeToParcel(Parcel dest, int flags) {
   dest.writeString(title);
   dest.writeParcelable((Parcelable)this.getLocation(), flags);
   dest.writeString(value);  
}

must be in the same order as:

private void readFromParcel(Parcel in){

  this.setTitle(in.readString());
  this.setLocation((Location)in.readParcelable(android.location.Location.class.getClassLoader()));
  this.setValue(in.readString());
}

Upvotes: 2

Alex
Alex

Reputation: 1345

As was pointed out this was an incorrect approach to use. So although I haven't solved it, there is a better way of doing the same thing. So consider it instead

How to declare global variables in Android?

Although if anyone has any idea for the answer to the original question please post. Can happen somewhere else to someone else.

Alex

Upvotes: 0

Related Questions