2

I need to send a list of objects from an activity to other activity with an Intent. I've implemented my class by Parcelable:

public class ListMainActivityHolder implements Parcelable {
String title ;
String date ;
String url ;
public ListMainActivityHolder(){}

public void setTitle(String t){
    this.title = t ;
}
public ListMainActivityHolder(Parcel source){
    title = source.readString();
    setDate(source.readString());
    url   = source.readString();

}

public void setDate(String d){

    if(d.length()>40){
        d = d.substring(0, 30);
        d = d + "..." ;
    }

    this.date = extractDate(d) ;
}

public void setUrl(String u){
    this.url = u ;
}   

public String getUrl(String u){
    return this.url ;
}

public String getTitle(){
    return this.title;
}

public String getDate(){
    return this.date ;
}




public String extractDate(String date){
    String day , month , year;
    if(date.length()>10){
    day   = date.substring(5,7);
    month = date.substring(8,11);
    year  = date.substring(12, 16);

    if(month.equalsIgnoreCase("Jan")){
        month = "01" ;
    }else if(month.equalsIgnoreCase("Feb")){
        month = "02" ;
    }else if(month.equalsIgnoreCase("Mar")){
        month = "03" ;
    }else if(month.equalsIgnoreCase("Apr")){
        month = "04" ;
    }else if(month.equalsIgnoreCase("May")){
        month = "05" ;
    }else if(month.equalsIgnoreCase("Jun")){
        month = "06" ;
    }else if(month.equalsIgnoreCase("Jul")){
        month = "07" ;
    }else if(month.equalsIgnoreCase("Aug")){
        month = "08" ;
    }else if(month.equalsIgnoreCase("Sep")){
        month = "09" ;
    }else if(month.equalsIgnoreCase("Oct")){
        month = "10" ;
    }else if(month.equalsIgnoreCase("Nov")){
        month = "11" ;
    }else if(month.equalsIgnoreCase("Dec")){
        month = "12" ;
    }

    }else{
        day   = "20" ;
        month = "02" ;
        year  = "2014" ;
    }

    CalendarTool calender = new CalendarTool(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
    String newdate = calender.getIranianDate();
    return newdate ;

}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeString(title);
    dest.writeString(date);
    dest.writeString(url);
}

public static final Parcelable.Creator<ListMainActivityHolder> CREATOR 

= new Parcelable.Creator<ListMainActivityHolder>() {

    @Override
    public ListMainActivityHolder createFromParcel(Parcel source) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public ListMainActivityHolder[] newArray(int size) {
        // TODO Auto-generated method stub
        return new ListMainActivityHolder[size];
    }
};
}

at my activity:

Intent intent = getIntent();
listholder = intent.getParcelableExtra("MYLIST");
String str = listholder.get(1).getDate();
Log.e("Tag", str);

when I running my app I get these errors :

03-06 08:47:47.876: E/AndroidRuntime(3401): FATAL EXCEPTION: main 03-06 08:47:47.876: E/AndroidRuntime(3401): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@433bd068: Unmarshalling unknown type code 39 at offset 132

David Wasser
  • 93,459
  • 16
  • 209
  • 274
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

1

In your CREATOR, you have this:

@Override
public ListMainActivityHolder createFromParcel(Parcel source) {
    // TODO Auto-generated method stub
    return null;
}

That means that whenever Android tries to unmarshal your class, you return null instead of an instance of ListMainActivityHolder.

You need to actually return an instance of ListMainActivityHolder like this:

@Override
public ListMainActivityHolder createFromParcel(Parcel source) {
    return new ListMainActivityHolder(source);
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

You can use android studio plug in for make class as parcelable https://plugins.jetbrains.com/plugin/7332?pr=

Problem is in sequence of writing and reading of object...sequence should be same, as it is like reading file.