android - 面对这个 java.lang.IllegalStateException: Bundle length is notaligned by 4: 9 in Model class which implement 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);
}
}
我想通过意图传递配置文件对象,所以我创建了实现 Parcelable 的模型类。这是我实现 Parcelable 的模型类。我的模型类包含两个布尔字段,其中我遇到了“isSelect”字段而不是“isPaused”字段的问题。不知道这是什么原因。我在网上搜索但没有得到任何解决方案。我看到的崩溃日志如下:
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$1.createFromParcel(Profile.java:41)
at com.centurylink.ctl_droid_wrap.models.Profile$1.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) 替换此代码,它可以正常工作
推荐阅读
- sparql - 如何在一次查询中获取属性标签
- android - 在谷歌地图中添加标记时,IONIC V4 应用程序关闭
- python - 如何在 alembic 升级中使用 op.execute
- arrays - 如何在本机反应中渲染对象?
- linux - 在 MAC 上安装 bcftools 时如何解决此错误?C 的问题:[ploidy.o] 错误 1
- python - 当您需要不同的参数时,如何在类中使用 if/elif?
- c# - C# WebBrowser HTML Get ByClass 不起作用
- google-cloud-platform - 如何获取在 Google Cloud Repositories 中推送新修改的历史信息?
- r - 如何将此长格式数据帧转换为宽格式?
- node.js - 是否可以仅下载附加 blob 的特定部分?