Android parcelable和Intent:redBundle:糟糕的幻数

时间:2012-04-11 07:36:50

标签: android android-intent bundle parcelable

我有一个对象的数组列表,我使用此示例将此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];
        }
        };

}

2 个答案:

答案 0 :(得分:3)

确保您的商品Article可以包含在内。如果没有,则实现Article的parcelable接口,然后尝试。 ArrayList是可以分配的,但是你放入它的对象也需要可以分配才能使它正常工作。

修改

将您的代码更改为此并尝试。您的写作顺序和阅读顺序不同。我认为这就是造成这个问题的原因。

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

答案 1 :(得分:0)

通过这个Blog,他解释了在两个活动之间传递arraylist