我需要使用Intent将活动中的对象列表发送到其他活动。我用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];
}
};
}
在我的活动中:
Intent intent = getIntent();
listholder = intent.getParcelableExtra("MYLIST");
String str = listholder.get(1).getDate();
Log.e("Tag", str);
当我运行我的应用时,我遇到了这些错误:
03-06 08:47:47.876:E / AndroidRuntime(3401):致命异常:主要 03-06 08:47:47.876:E / AndroidRuntime(3401): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.test / com.example.test.MainActivity}: java.lang.RuntimeException:Parcel android.os.Parcel@433bd068: 在偏移132处解组未知类型代码39
答案 0 :(得分:1)
在CREATOR
中,你有这个:
@Override
public ListMainActivityHolder createFromParcel(Parcel source) {
// TODO Auto-generated method stub
return null;
}
这意味着每当Android尝试解组您的课程时,您都会返回null
而不是ListMainActivityHolder
的实例。
您需要实际返回ListMainActivityHolder
的实例,如下所示:
@Override
public ListMainActivityHolder createFromParcel(Parcel source) {
return new ListMainActivityHolder(source);
}
答案 1 :(得分:0)
你可以使用android studio plug for make class作为parcelable https://plugins.jetbrains.com/plugin/7332?pr=
问题是按顺序写入和读取对象...序列应该是相同的,就像读取文件一样。