Reputation: 129
Some times, I got this error, i don't know what is wrong in my code Please help!
Any suggestion code it will help.
Here it is my error report:
com.example E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.MainActivity}: android.os.BadParcelableException: Parcelable protocol requires that the class implements Parcelable
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.os.BadParcelableException: Parcelable protocol requires that the class implements Parcelable
at android.os.Parcel.readParcelableCreator(Parcel.java:2407)
at android.os.Parcel.readParcelable(Parcel.java:2358)
at android.view.AbsSavedState.<init>(AbsSavedState.java:57)
at android.view.View$BaseSavedState.<init>(View.java:22902)
at com.example.DrawableViewSaveState.<init>(DrawableViewSaveState.java:17)
at com.example.DrawableViewSaveState$1.createFromParcel(DrawableViewSaveState.java:43)
at com.example.DrawableViewSaveState$1.createFromParcel(DrawableViewSaveState.java:41)
And Here it is my DrawableViewSaveState class:
And this error happend when i want go back to my acivity that use the DrawableViewSaveState class. I'm always getting the crash in super(in);
public class DrawableViewSaveState extends View.BaseSavedState {
private ArrayList<SerializablePath> paths;
public DrawableViewSaveState(Parcel in) {
super(in); // I'm always getting the crash in this line of code.
this.paths = (ArrayList<SerializablePath>) in.readSerializable();
for (SerializablePath p : paths) {
p.loadPathPointsAsQuadTo();
}
}
public DrawableViewSaveState(Parcelable parcelable) {
super(parcelable);
}
@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeSerializable(this.paths);
}
public ArrayList<SerializablePath> getPaths() {
return paths;
}
public void setPaths(ArrayList<SerializablePath> paths) {
this.paths = paths;
}
public static final Creator<DrawableViewSaveState> CREATOR =
new Creator<DrawableViewSaveState>() {
public DrawableViewSaveState createFromParcel(Parcel source) {
return new DrawableViewSaveState(source);
}
public DrawableViewSaveState[] newArray(int size) {
return new DrawableViewSaveState[size];
}
};
}
Upvotes: 1
Views: 1824
Reputation: 4222
The constructor of parent class attempts to read Parcelable from Parcel. Unfortunately (or perhaps fortunately — at least you avoided silent data corruption!), Parcel does not contain expected Parcelable.
The reason is because you are violating expectation of the parent class. As can be seen from source code of AbsSavedState, the constructor, that accepts a Parcel
, expects to see a Parcelable, received by your DrawableViewSaveState(Parcelable parcelable)
constructor, — e.g. the state of parent View
class):
protected AbsSavedState(Parcel source) {
// FIXME need class loader
Parcelable superState = source.readParcelable(null);
mSuperState = superState != null ? superState : EMPTY_STATE;
}
That Parcelable is written to Parcel by writeToParcel()
of your superclass, but you are not calling it!
Solution: add call to super.writeToParcel(dest, flags);
to the beginning of your writeToParcel
method.
Future lesson: if you inherit from Parcelable classes, consider calling writeToParcel of superclass from your writeToParcel method.
Upvotes: 1