本文介绍了Android的parcelable和意图:redBundle:坏的幻数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象的数组列表,我用这个例子来得到这个ArrayList中从一个活动到另一个活动:的

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

下面是我的code的一部分:

Here is a part of my code:

活动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");

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

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

这是我第三类:

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

}

推荐答案

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

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.

修改

更改code到这一点,并尝试。你的写作顺序和读取顺序是不同的。我认为这是什么原因造成你的问题。

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

这篇关于Android的parcelable和意图:redBundle:坏的幻数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:46