Android中捆绑包的错误幻数

2022-09-04 22:09:34

我使用以下代码将数据从一个活动传递到另一个活动:

 @Override
    public void execute(List<Report> reports, Questions question) {
        Intent replyIntent = new Intent(listener, ReplyActivity.class);
        replyIntent.putExtra("id", 0L);
        replyIntent.putExtra("questions", question);
        listener.openReportOk(question);
        listener.startActivity(replyIntent);
    }

侦听器 它是回调的活动引用。

问题是这门课:

@Table(name = "Questions")
public class Questions extends Entity implements Parcelable {

    public static final Creator<Questions> CREATOR = new Creator<Questions>() {
        public Questions createFromParcel(Parcel source) {
            return new Questions(source);
        }

        public Questions[] newArray(int size) {
            return new Questions[size];
        }
    };

    @TableField(name = "idReport", datatype = DATATYPE_INTEGER)
    private int idReport;
    @TableField(name = "nameReport", datatype = DATATYPE_STRING)
    private String nameReport;
    @TableField(name = "replyGroups", datatype = DATATYPE_STRING)
    private String replyGroups;
    @TableField(name = "questionGroups", datatype = DATATYPE_ENTITY)
    private List<QuestionsGroup> questionsGroup;
    private Boolean canCreateNew;

    public Questions(int idReport, String nameReport, String replyGroups, List<QuestionsGroup> questionsGroup) {
        this.idReport = idReport;
        this.nameReport = nameReport;
        this.replyGroups = replyGroups;
        this.questionsGroup = questionsGroup;
        this.canCreateNew = false;
    }

    public Questions() {
        questionsGroup = new ArrayList<QuestionsGroup>();
    }

    private Questions(Parcel in) {
        this();
        this.idReport = in.readInt();
        this.nameReport = in.readString();
        this.replyGroups = in.readString();
        Bundle b = in.readBundle(QuestionsGroup.class.getClassLoader());
        this.questionsGroup = b.getParcelableArrayList("questionGroups");
        this.canCreateNew = (Boolean) in.readValue(Boolean.class.getClassLoader());
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.idReport);
        dest.writeString(this.nameReport);
        dest.writeString(this.replyGroups);
        Bundle b = new Bundle();
        b.putParcelableArrayList("questionGroups", (ArrayList<QuestionsGroup>) this.questionsGroup);
        dest.writeBundle(b);
        dest.writeValue(this.canCreateNew);
    }
}

当我在 onCreate 方法中接收包裹时:

 private void getData(Intent data) {
        ID = data.getExtras().getLong("id");
        questions = data.getExtras().getParcelable("questions");
    }

我收到此错误:

10-05 13:19:15.508    3499-3499/com.firext.android E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.firext.android, PID: 3499
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firext.android/com.firext.android.activities.reply.ReplyActivity}: java.lang.IllegalStateException: Bad magic number for Bundle: 0x28
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
     Caused by: java.lang.IllegalStateException: Bad magic number for Bundle: 0x28
            at android.os.BaseBundle.readFromParcelInner(BaseBundle.java:1342)
            at android.os.BaseBundle.<init>(BaseBundle.java:90)
            at android.os.Bundle.<init>(Bundle.java:66)
            at android.os.Parcel.readBundle(Parcel.java:1645)
            at com.firext.android.domain.QuestionsGroup.<init>(QuestionsGroup.java:53)
            at com.firext.android.domain.QuestionsGroup$1.createFromParcel(QuestionsGroup.java:25)
            at com.firext.android.domain.QuestionsGroup$1.createFromParcel(QuestionsGroup.java:23)
            at android.os.Parcel.readParcelable(Parcel.java:2160)
            at android.os.Parcel.readValue(Parcel.java:2066)
            at android.os.Parcel.readListInternal(Parcel.java:2422)
            at android.os.Parcel.readArrayList(Parcel.java:1756)
            at android.os.Parcel.readValue(Parcel.java:2087)
            at android.os.Parcel.readArrayMapInternal(Parcel.java:2393)
            at android.os.BaseBundle.unparcel(BaseBundle.java:221)
            at android.os.Bundle.getParcelableArrayList(Bundle.java:782)
            at com.firext.android.domain.Questions.<init>(Questions.java:58)
            at com.firext.android.domain.Questions.<init>(Questions.java:18)
            at com.firext.android.domain.Questions$1.createFromParcel(Questions.java:22)
            at com.firext.android.domain.Questions$1.createFromParcel(Questions.java:20)
            at android.os.Parcel.readParcelable(Parcel.java:2160)
            at android.os.Parcel.readValue(Parcel.java:2066)
            at android.os.Parcel.readArrayMapInternal(Parcel.java:2393)
            at android.os.BaseBundle.unparcel(BaseBundle.java:221)
            at android.os.Bundle.getParcelable(Bundle.java:738)
            at com.firext.android.activities.reply.ReplyActivity.getData(ReplyActivity.java:72)
            at com.firext.android.activities.reply.ReplyActivity.onCreate(ReplyActivity.java:38)
            at android.app.Activity.performCreate(Activity.java:5720)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
            at android.app.ActivityThread.access$800(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5070)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)

怎么了?


答案 1

总结

看起来您在存储和检索 的打包信息的方式上存在错误。53号线是要注意的线路。QuestionsGroup

在写入包裹时,Android 会在每个字段中存储一个特殊的“幻数”,以确保您在从包裹中取出字段时使用正确的顺序(有关编号的定义,请参阅详细说明)。

您收到错误是因为在预期位置找不到幻数 - 这是因为您没有以正确的顺序从包裹中取出字段。


来自官方源代码的详细说明

[ 下面的解释是基于“最新”(在撰写本文时)API21 实现的 和 类。您可以在此处找到完整的代码:BundleBaseBundle

您还可以使用 Android SDK 管理器将代码的本地副本安装到开发计算机上,我发现这非常有用。]

所以。。。

写入宗地时,Android 会采取一些安全措施,以确保在您尝试取消包裹时字段对齐。为此,Android 会在字段后附加一个“幻数”,其定义为:

static final int BUNDLE_MAGIC = 0x4C444E42; // 'B' 'N' 'D' 'L'

您可以在 的代码中看到这一点(总结如下)writeToParcelInner()

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

    }
}

同样,在读取包裹时,Android 会在返回包裹中的值之前检查该数字。BUNDLE_MAGIC

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

}

在上面的 API21 BaseBundle 代码中,您可以看到您的错误根据 logcat 跟踪被抛出在第 1342 行。


解决方法和进一步调试

原始海报说明这是正确可包裹的。QuestionsGroup

进一步调试此问题的一种方法是远离(当前)引入该类的新API21;例如,下降到API19,它只使用该类。BaseBundleBundle

如果用户的代码实际上是正确的,那么它更有可能在更成熟的API上工作。

如果代码仍然中断,则可能是代码。但是,此外,新的 logcat 跟踪将指向代码的不同部分,可以查看该部分,并且可以更深入地了解问题。QuestionsGroup

除非它是 Bundle,第 1730 行,从方法 ,这将意味着相同的问题:readFromParcelInner()

    int magic = parcel.readInt();
    if (magic != BUNDLE_MAGIC) {
        //noinspection ThrowableInstanceNeverThrown
        throw new IllegalStateException("Bad magic number for Bundle: 0x"
                + Integer.toHexString(magic));
    }

相关文章

有关相关信息,请参阅以下帖子:


答案 2

说实话,我从来没有遇到过这样的错误。但是我使用另一种方式将()存储在我的()中:listparcelableQuestionsGroupparcelableQuestions

   private Questions(Parcel in) {
        this();
        this.idReport = in.readInt();
        this.nameReport = in.readString();
        this.replyGroups = in.readString();
        //Bundle b = in.readBundle(QuestionsGroup.class.getClassLoader());
        //this.questionsGroup = b.getParcelableArrayList("questionGroups");
        this.questionsGroup = in.readTypedList(questionsGroup,QuestionsGroup.CREATOR);
        this.canCreateNew = (Boolean) in.readValue(Boolean.class.getClassLoader());
    }

和:

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(this.idReport);
    dest.writeString(this.nameReport);
    dest.writeString(this.replyGroups);
    //Bundle b = new Bundle();
    //b.putParcelableArrayList("questionGroups", (ArrayList<QuestionsGroup>) this.questionsGroup);
    //dest.writeBundle(b);
    dest.writeTypedList(questionsGroup);
    dest.writeValue(this.canCreateNew);
}

我在我的项目中使用上面的代码,它工作正常。您可能想尝试一下,因为它既简单又快捷。


推荐