安卓parcelable和意图:redBundle:坏的幻数(Android parcelable

2019-09-27 09:30发布

我有对象的数组列表,我用这个例子来获得此ArrayList从一个活动到另一个活动: http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html

这里是我的代码的一部分:

活动1:

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

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

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

活动2:

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

但在这里,我得到一个错误,这里是日志:

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

下面是我的文章类:

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];
        }
        };

}

Answer 1:

请确保您的项目Article是parcelable。 如果没有,那么实施的条界面parcelable,然后尝试。 ArrayList的是parcelable但你把它的对象必须是parcelable过它才能正常工作。

编辑

你的代码更改为这个和尝试。 你的写作顺序和读取顺序是不同的。 我认为这是什么原因造成你的问题。

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();


Answer 2:

通过这次访问博客 ,他大约传递两个活动之间的ArrayList解释



文章来源: Android parcelable and Intent: redBundle: bad magic number