面对这个 java.lang.IllegalStateException:在实现 Parcelable 的模型 class 中,捆绑长度未按 4:9 对齐
Facing this java.lang.IllegalStateException: Bundle length is not aligned by 4: 9 in Model class which implements Parcelable
public class Profile implements Parcelable {
@SerializedName("profileName")
private String profileName;
@SerializedName("paused")
private boolean isPaused;
@SerializedName("numberOfDevices")
private int numberOfDevices;
private boolean isSelected;
public Profile(String profileName, Boolean isPaused, int
numberOfDevices) {
this.profileName = profileName;
this.isPaused = isPaused;
this.numberOfDevices = numberOfDevices;
}
protected Profile(Parcel in) {
profileName = in.readString();
isPaused = (boolean)
in.readValue(boolean.class.getClassLoader());
isSelected = (boolean)
in.readValue(boolean.class.getClassLoader());
numberOfDevices = in.readInt();
}
public static final Creator<Profile> CREATOR = new Creator<Profile>
.() {
@Override
public Profile createFromParcel(Parcel in) {
return new Profile(in);
}
@Override
public Profile[] newArray(int size) {
return new Profile[size];
}
};
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
public Boolean getPaused() {
return isPaused;
}
public void setPaused(Boolean paused) {
isPaused = paused;
}
public int getNumberOfDevices() {
return numberOfDevices;
}
public void setNumberOfDevices(int numberOfDevices) {
this.numberOfDevices = numberOfDevices;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(profileName);
dest.writeValue(isPaused);
dest.writeInt(numberOfDevices);
dest.writeValue(isSelected);
}
}
I would like to pass the profile object through intents so I created the model class which implements Parcelable. This is my Model class which implements Parcelable. My model class
contains two boolean fields where I am having an issue with "isSelect"
field and not with "isPaused" field. Don't know the reason for this. I
searched online but didn't get any solution for this. The crash logs I
see is as below:
java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
at android.os.BaseBundle.readFromParcelInner(BaseBundle.java:1615)
at android.os.BaseBundle.<init>(BaseBundle.java:143)
at android.os.Bundle.<init>(Bundle.java:95)
at android.os.Parcel.readBundle(Parcel.java:2301)
at android.os.Parcel.readValue(Parcel.java:2946)
at com.centurylink.ctl_droid_wrap.models.Profile.<init>(Profile.java:34)
at com.centurylink.ctl_droid_wrap.models.Profile.createFromParcel(Profile.java:41)
at com.centurylink.ctl_droid_wrap.models.Profile.createFromParcel(Profile.java:38)
at android.os.Parcel.readParcelable(Parcel.java:2990)
at android.os.Parcel.readValue(Parcel.java:2883)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3261)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
at android.os.BaseBundle.unparcel(BaseBundle.java:236)
at android.os.Bundle.getParcelable(Bundle.java:951)
protected Profile(Parcel in) {
profileName = in.readString();
isPaused = in.readByte() != 0;
numberOfDevices = in.readInt();
isSelected = in.readByte() != 0;
}
如果我用现有的 Profile(Parcel in) 替换此代码,它可以正常工作
public class Profile implements Parcelable {
@SerializedName("profileName")
private String profileName;
@SerializedName("paused")
private boolean isPaused;
@SerializedName("numberOfDevices")
private int numberOfDevices;
private boolean isSelected;
public Profile(String profileName, Boolean isPaused, int
numberOfDevices) {
this.profileName = profileName;
this.isPaused = isPaused;
this.numberOfDevices = numberOfDevices;
}
protected Profile(Parcel in) {
profileName = in.readString();
isPaused = (boolean)
in.readValue(boolean.class.getClassLoader());
isSelected = (boolean)
in.readValue(boolean.class.getClassLoader());
numberOfDevices = in.readInt();
}
public static final Creator<Profile> CREATOR = new Creator<Profile>
.() {
@Override
public Profile createFromParcel(Parcel in) {
return new Profile(in);
}
@Override
public Profile[] newArray(int size) {
return new Profile[size];
}
};
public String getProfileName() {
return profileName;
}
public void setProfileName(String profileName) {
this.profileName = profileName;
}
public Boolean getPaused() {
return isPaused;
}
public void setPaused(Boolean paused) {
isPaused = paused;
}
public int getNumberOfDevices() {
return numberOfDevices;
}
public void setNumberOfDevices(int numberOfDevices) {
this.numberOfDevices = numberOfDevices;
}
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(profileName);
dest.writeValue(isPaused);
dest.writeInt(numberOfDevices);
dest.writeValue(isSelected);
}
}
I would like to pass the profile object through intents so I created the model class which implements Parcelable. This is my Model class which implements Parcelable. My model class contains two boolean fields where I am having an issue with "isSelect" field and not with "isPaused" field. Don't know the reason for this. I searched online but didn't get any solution for this. The crash logs I see is as below:
java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3447)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3594)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2146)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)
Caused by: java.lang.IllegalStateException: Bundle length is not aligned by 4: 9
at android.os.BaseBundle.readFromParcelInner(BaseBundle.java:1615)
at android.os.BaseBundle.<init>(BaseBundle.java:143)
at android.os.Bundle.<init>(Bundle.java:95)
at android.os.Parcel.readBundle(Parcel.java:2301)
at android.os.Parcel.readValue(Parcel.java:2946)
at com.centurylink.ctl_droid_wrap.models.Profile.<init>(Profile.java:34)
at com.centurylink.ctl_droid_wrap.models.Profile.createFromParcel(Profile.java:41)
at com.centurylink.ctl_droid_wrap.models.Profile.createFromParcel(Profile.java:38)
at android.os.Parcel.readParcelable(Parcel.java:2990)
at android.os.Parcel.readValue(Parcel.java:2883)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3261)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
at android.os.BaseBundle.unparcel(BaseBundle.java:236)
at android.os.Bundle.getParcelable(Bundle.java:951)
protected Profile(Parcel in) {
profileName = in.readString();
isPaused = in.readByte() != 0;
numberOfDevices = in.readInt();
isSelected = in.readByte() != 0;
}
如果我用现有的 Profile(Parcel in) 替换此代码,它可以正常工作