Android parcelable and Intent: redBundle: bad magic number

会有一股神秘感。 提交于 2019-12-11 04:51:27

问题


I have an array list of objects and I am using this example to get this arrayList from one activity to another activity: http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html

Here is a part of my code:

Activity 1:

Intent myIntent = new Intent(context, JournalArticles.class);

         Bundle b = new Bundle();
         b.putParcelableArrayList("articles", articles);

         myIntent.putExtras(b);
         startActivityForResult(myIntent,0);

Activity 2:

Bundle b = this.getIntent().getExtras();
ArrayList<Article> articles = b.getParcelableArrayList("articles");

But here I am getting an error, here is the log:

04-11 13:35:03.548: D/AndroidRuntime(2686): Shutting down VM
04-11 13:35:03.548: W/dalvikvm(2686): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
04-11 13:35:03.598: E/AndroidRuntime(2686): FATAL EXCEPTION: main
04-11 13:35:03.598: E/AndroidRuntime(2686): java.lang.RuntimeException: Unable to start activity ComponentInfo{milos.mdpi/milos.mdpi.JournalArticles}: java.lang.RuntimeException: Parcel android.os.Parcel@4055e378: Unmarshalling unknown type code 6357106 at offset 196
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread.access$1500(ActivityThread.java:135)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Looper.loop(Looper.java:150)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread.main(ActivityThread.java:4389)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at java.lang.reflect.Method.invokeNative(Native Method)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at java.lang.reflect.Method.invoke(Method.java:507)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at dalvik.system.NativeStart.main(Native Method)
04-11 13:35:03.598: E/AndroidRuntime(2686): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@4055e378: Unmarshalling unknown type code 6357106 at offset 196
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Parcel.readValue(Parcel.java:1913)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Parcel.readListInternal(Parcel.java:2092)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Parcel.readArrayList(Parcel.java:1536)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Parcel.readValue(Parcel.java:1867)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Parcel.readMapInternal(Parcel.java:2083)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Bundle.unparcel(Bundle.java:208)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at milos.mdpi.JournalArticles.onCreate(JournalArticles.java:138)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-11 13:35:03.598: E/AndroidRuntime(2686):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
04-11 13:35:03.598: E/AndroidRuntime(2686):     ... 11 more

Here is my Article class:

public class Article implements Parcelable

{

private Integer _ArticleID;
public Integer getArticleID(){
    return _ArticleID;
}
public void setArticleID(Integer value){
    _ArticleID = value;
}
private Integer _JournalID;
public Integer getJournalID(){
    return _JournalID;
}
public void setJournalID(Integer value){
    _JournalID = value;
}
private Integer _Volume;
public Integer getVolume(){
    return _Volume;
}
public void setVolume(Integer value){
    _Volume = value;
}
private Integer _Issue;
public Integer getIssue(){
    return _Issue;
}
public void setIssue(Integer value){
    _Issue = value;
}
private Integer _Firstpage;
public Integer getFirstpage(){
    return _Firstpage;
}
public void setFirstpage(Integer value){
    _Firstpage = value;
}
private Integer _Lastpage;
public Integer getLastpage(){
    return _Lastpage;
}
public void setLastpage(Integer value){
    _Lastpage = value;
}
private String _PublishDate;
public String getPublishDate(){
    return _PublishDate;
}
public void setPublishDate(String value){
    _PublishDate = value;
}
private String _Title;
public String getTitle(){
    return _Title;
}
public void setTitle(String value){
    _Title = value;
}
private String _Abstract;
public String getAbstract(){
    return _Abstract;
}
public void setAbstract(String value){
    _Abstract = value;
}
private String _Keywords;
public String getKeywords(){
    return _Keywords;
}
public void setKeywords(String value){
    _Keywords = value;
}



public int describeContents() {
return 0;
}

public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(_ArticleID);
    dest.writeInt(_JournalID);
    dest.writeInt(_Volume);
    dest.writeInt(_Issue);
    dest.writeInt(_Firstpage);
    dest.writeInt(_Lastpage);
    dest.writeString(_PublishDate);
    dest.writeString(_Title);
    dest.writeString(_Abstract);
    dest.writeString(_Keywords);
}


public static final Parcelable.Creator<Article> CREATOR = 
        new Parcelable.Creator<Article>() { 
        public Article createFromParcel(Parcel in) { 
            Article article = new Article();
            article._Abstract = in.readString();
            article._ArticleID = in.readInt();
            article._Firstpage = in.readInt();
            article._Issue = in.readInt();
            article._JournalID = in.readInt();
            article._Keywords = in.readString();
            article._Lastpage = in.readInt();
            article._PublishDate = in.readString();
            article._Title = in.readString();
            article._Volume = in.readInt();      

        return article;
        }

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

}


回答1:


Make sure that your item Article is parcelable. If not then implement the parcelable interface for Article and then try. ArrayList is parcelable but the object you put in it needs to be parcelable too for it to work properly.

Edit

Change your code to this and try. Your writing order and reading order were different. I think this is what is causing you the problem.

article._ArticleID = in.readInt();
article._JournalID = in.readInt();
article._Volume = in.readInt();
article._Issue = in.readInt();
article._Firstpage = in.readInt();
article._Lastpage = in.readInt();
article._PublishDate = in.readString();
article._Title = in.readString();
article._Abstract = in.readString();
article._Keywords = in.readString();



回答2:


Go through this Blog where he explained about passing arraylist between two activities



来源:https://stackoverflow.com/questions/10101771/android-parcelable-and-intent-redbundle-bad-magic-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!