1

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

}

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

2 Answers2

3

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();
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • Thank you for the answer @Shubhayu. The class Article is allready parcelable. – Milos Cuculovic Apr 11 '12 at 07:44
  • hmmmm so you have implemeneted it as per http://developer.android.com/reference/android/os/Parcelable.html? Surely seems like a problem with how you have implemented it based upoon this line in your logcat 04-11 09:31:09.550: E/Bundle(28775): at milos.mdpi.clientcode.Article$1.createFromParcel(Article.java:181) Maybe us hsould post the code of Article. – Shubhayu Apr 11 '12 at 08:07
  • I added the Article class to my question, thanks again for your help. – Milos Cuculovic Apr 11 '12 at 08:22
  • I have changed the createFromParcel() part of your code. See if it works. – Shubhayu Apr 11 '12 at 08:52
  • Thank you @Shubhayu, I have another error now, I modified my log – Milos Cuculovic Apr 11 '12 at 11:35
0

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

Abhi
  • 8,935
  • 7
  • 37
  • 60