1

My int variables are not reading/writing correctly in my Parcelable object. I am passing my object from one activity to another but the int variables are not the same once I pass it and read it.

Heres my code.

PARCELABLE OBJECT:

class someObject implements Parcelable
int something;
int something2;

//normal constructor
... 

public someObject(Parcel in){
  this.something = in.readInt();
  this.something2 = in.readInt();
}

public void writeToParcel(Parcel dest, int flags){
   dest.writeInt(something);
   dest.writeInt(something2);
}
//creator and other parcel stuff
...

//mutators for int variables
...

How I passed it in activity1:

//instantiate someObject 
...
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtras("stuff", someObject);

Passed to activity2:

onCreate(Bundle blah){
Bundle b = getIntent.getExtras();
someObject some = b.getParcelable("stuff");

}

I output something and something2 within the constructor and the activity that I pass it too. They are different values, what did I do wrong?

I have other String member variables in my parcelable object class and they seem to work. Just the 2 ints are not working properly.

Instinct
  • 2,201
  • 1
  • 31
  • 45
  • You mean that `something` is different when you create a new `someObject`, and when you receive that object in your `activity2#onCreate()` method? Not that `something` is different from `something2`, right? – Nate Jun 02 '13 at 21:29
  • Exactly, I pass something and something2 into the original constructor in activity1 then try to retrieve it in activity2. But something in activity1 != something in activity2. Same for something2 – Instinct Jun 02 '13 at 21:32
  • The code you show looks fine to me. Perhaps there's something elsewhere that's messing things up? It's really easy to get cut-and-paste errors when you have things named like `something` and `something2`. Maybe a *fat-finger* error? – Nate Jun 02 '13 at 21:34
  • Also, I can already see that you made one error when copying your code into your question above. There is no method `intent.putExtras("stuff", someObject)`. There is `intent#putExtras(Bundle)` or `intent#putExtra(String, Parcelable)`. – Nate Jun 02 '13 at 21:38

2 Answers2

1

From documentation:

final int    readInt()

Read an integer value from the parcel at the current dataPosition().

The current dataPosition() means that the two in.readInt() are different.

(Here: http://developer.android.com/reference/android/os/Parcel.html)

Paolof76
  • 889
  • 1
  • 9
  • 23
1

I found the problem, my write and read were not in the same position so the dataPosition() was off and thus reading different objects.

Ex)

 read(parcel in){
   in.readString(); //1
   in.readString(); //2
   in.readInt();    //3
}

 write(Parcel dest, int flag){
   dest.writeString(); //1
   dest.writeInt();    //3
   dest.writeString(); //2
}
Instinct
  • 2,201
  • 1
  • 31
  • 45
  • That's why you shouldn't post questions with code that isn't actually the code you're using, and having problems with. – Nate Jun 02 '13 at 21:40