gpt4 book ai didi

android - 解码未知类型代码android

转载 作者:行者123 更新时间:2023-11-29 01:44:42 25 4
gpt4 key购买 nike

我需要将对象列表从一个 Activity 发送到具有 Intent 的其他 Activity 。我已经通过 Parcelable 实现了我的类(class):

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

在我的 Activity 中:

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): 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

最佳答案

在你的 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);
}

关于android - 解码未知类型代码android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22215822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com