0

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

Alex
  • 1,315
  • 4
  • 16
  • 29
  • 2
    Are you trying to pass a ListAdapter implementation back as a result? (The variable name `la` looks suspicious to me. :)) – Ted Hopp Jun 12 '11 at 20:42
  • Well yes and no. I am passing back the data held in the adapter. So it flattens various bits of data inside yes. It works for the configuration change where I do exactly the same thing but I stick it in to the savedInstance instead of Intent. Is that a problem? I thought I am just flattening the data inside thats it. – Alex Jun 13 '11 at 10:33

3 Answers3

2

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());
}
0

Does Intent.setExtrasClassLoader(java.lang.ClassLoader) help?

Tavian Barnes
  • 12,477
  • 4
  • 45
  • 118
0

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

Community
  • 1
  • 1
Alex
  • 1,315
  • 4
  • 16
  • 29
  • This is happening in my code.. Everything looks ok, but crashes when reading it back... – Ron Sep 18 '12 at 07:25