Summary
It looks like you have an error in how you store & retrieve the parcelled information of QuestionsGroup
. Line 53 is the line to pay attention to.
When writing to the parcel, Android stores a special "magic number" with each field, to ensure that you use the correct order when pulling fields out of the parcel (see detailed explanation for the definition of the number).
You are getting the error because the magic number is not found in the expected position - this is because you are not taking the fields out of the parcel in the correct order.
Detailed Explanation from Official Source Code
[ Below explanation is based on the "latest" (at time of writing) API21 implementation of the Bundle
and BaseBundle
classes. You can find the full code here:
You can also use the Android SDK Manager to install a local copy of the code onto your development machine, which I find very useful. ]
So...
When parcels are written, Android takes some security steps to ensure that the fields line up when you try to unparcel them. To do this, Android appends the fields with a "magic number", defined as:
static final int BUNDLE_MAGIC = 0x4C444E42; // 'B' 'N' 'D' 'L'
You can see this in the code for writeToParcelInner()
(summarised below)
void writeToParcelInner(Parcel parcel, int flags) {
if (mParcelledData != null) {
if (mParcelledData == EMPTY_PARCEL) {
parcel.writeInt(0);
} else {
int length = mParcelledData.dataSize();
parcel.writeInt(length);
parcel.writeInt(BUNDLE_MAGIC);
parcel.appendFrom(mParcelledData, 0, length);
}
} else {
.......... extra code chopped out for illustration purposes
}
}
Similarly, when reading from the parcel, Android checks for that BUNDLE_MAGIC
number before returning the values from the parcel.
private void readFromParcelInner(Parcel parcel, int length) {
if (length == 0) {
// Empty Bundle or end of data.
mParcelledData = EMPTY_PARCEL;
return;
}
int magic = parcel.readInt();
if (magic != BUNDLE_MAGIC) {
//noinspection ThrowableInstanceNeverThrown
throw new IllegalStateException("Bad magic number for Bundle: 0x"
+ Integer.toHexString(magic));
}
.......... extra code chopped out for illustration purposes
}
In the above code for API21 BaseBundle, you can see your error being thrown on line 1342 as per the logcat trace.
Workaround & Further Debugging
Original poster states that QuestionsGroup
is correctly parcelable.
One way to further debug this issue would be to move away from the (currently) new API21, which introduced the BaseBundle
class; down to API19 for example which just uses the Bundle
class.
If the user's code is actually correct, then it is more likely to work on a more mature API.
If the code still breaks, it is likely the QuestionsGroup
code. However, in addition the new logcat trace will point to a different part of the code, which can be looked at and may provide more insight into the problem.
Unless it is to Bundle, line 1730, from method readFromParcelInner()
, which would imply the same problem:
int magic = parcel.readInt();
if (magic != BUNDLE_MAGIC) {
//noinspection ThrowableInstanceNeverThrown
throw new IllegalStateException("Bad magic number for Bundle: 0x"
+ Integer.toHexString(magic));
}
Related Posts
See the following posts for related information: