编组和解编组列表<;列表<;T>&燃气轮机;使用Android';包裹
当我试图解组一个编组和解编组列表<;列表<;T>&燃气轮机;使用Android';包裹,android,marshalling,unmarshalling,parcelable,Android,Marshalling,Unmarshalling,Parcelable,当我试图解组一个列表时,我得到了一个ClassNotFoundException,该列表位于这里的线程和所有关于答案的注释之后: 也许有意思的是: 将边框声明为数组列表(不是列表) 使用parcel.writeTypedList(List)写入边框列表 使用parcel.createTypedArrayList从包裹中读取它。看起来,readArrayList很难处理 添加一个以parcel为参数的构造函数,并将创建者添加到类中 public Country(Parcel source) {
列表时,我得到了一个ClassNotFoundException
,该列表位于这里的线程和所有关于答案的注释之后:
也许有意思的是:
- 将边框声明为数组列表(不是列表)
- 使用
parcel.writeTypedList(List)
写入边框列表
- 使用
parcel.createTypedArrayList
从包裹中读取它。看起来,readArrayList
很难处理
添加一个以parcel为参数的构造函数,并将创建者添加到类中
public Country(Parcel source) {
country.name = source.readString();
country.borders = source.readArrayList(Country.class
.getClassLoader());
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Country createFromParcel(Parcel in) {
return new Country(in);
}
public Country[] newArray(int size) {
return new Country[size];
}
};
编辑:
确保LatLng
实现Parcelable
。否则,当活动必须对其进行marshall/unmarshallCountry
时,它将不知道如何对其进行marshall和unmarshall(这就是您可能会遇到错误的原因:推断的类型列表不是有界参数的有效替代品
这应该是可行的-创建一个类来扩展ArrayList
。这个想法是使用ParcelableArrayList
(下面的类)的CREATOR
来创建您的列表。只有当您将列表实例化为ArrayList
(您应该做的另一个回答)时,它才会起作用
编辑2:
好的,请尝试使用Parcel#readTypedList()
,而不是使用Parcel#readList()
。因此,请替换:
country.borders = new ArrayList<ParcelableArrayList()>();
country.borders = source.readTypedList(ParcelableArrayList.CREATOR);
country.borders=new ArrayList();
country.borders=source.readTypedList(ParcelableArrayList.CREATOR);
与
country.borders=new ArrayList();
country.borders=source.readList(country.borders,ParcelableArrayList.class.getClassLoader());
我在自己的一个应用程序中对此进行了测试,它没有编译时或运行时错误(据我所知)。我通过将列表
分解为列表
其中CustomClass扩展了ArrayList>
这是我的自定义类代码
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class Lister extends ArrayList<AlertEvaluation> implements Parcelable {
protected Lister(Parcel in) {
this.addAll(in.readArrayList(AlertEvaluation.class.getClassLoader()));
}
public static final Creator<Lister> CREATOR = new Creator<Lister>() {
@Override
public Lister createFromParcel(Parcel in) {
return new Lister(in);
}
@Override
public Lister[] newArray(int size) {
return new Lister[size];
}
};
@Override public int describeContents() {
return 0;
}
@Override public void writeToParcel(Parcel parcel, int flags) {
parcel.writeList(this);
}
}
import java.util.ArrayList;
导入android.os.packet;
导入android.os.Parcelable;
公共类Lister扩展ArrayList实现Parcelable{
受保护列表器(包裹在中){
this.addAll(在.readArrayList(AlertEvaluation.class.getClassLoader())中);
}
公共静态最终创建者=新创建者(){
@凌驾
公共列表器createFromParcel(中的地块){
返回新列表器(在中);
}
@凌驾
公共列表器[]新数组(整数大小){
返回新列表器[大小];
}
};
@重写公共int描述符内容(){
返回0;
}
@覆盖公共无效writeToParcel(地块,int标志){
包裹。书面清单(本);
}
}
您能否显示启动编组/解编序列的代码,即codeguru.worldtour.MainActivity.onCreate第63行?这看起来像是IPC问题。@Snicolas您的愿望是我的命令您可以编辑您的代码以包含LatLng
类吗?@TrustLatLng
来自Google Maps v2 APIwriteTypedList()
给出编译器错误:绑定不匹配:Parcel类型的泛型方法writeTypedList(List)不适用于参数(List)。推断的类型列表不是有界参数的有效替代品。难道不能用parcelabel数据结构替换列表列表吗?当我在intent中发送类时,这会起作用。我还没有用数组或readArrayList尝试过这一点。这与我的OP中发布的代码有根本性的不同吗?我看不到这一点;然而,为了可读性,我会编写一个单独的构造函数(似乎是遵循的惯例)。我从步骤调试中知道,在解组中使用了带包的构造函数。@danny117 AFAICT,Country(parcel)
构造函数用于解组,因为您从createFromParcel()显式调用它
。我无法控制LatLng
类。它来自Google Maps API。从这里()我看到LatLng
已经实现了Parcelable
,所以您可以开始了。您是否尝试过使用上述方法?“推断的类型列表不是有界参数的有效替代品。”是由于T
引用的是列表(即,我正试图整理一个列表)。请参阅我的编辑。readList
而不是readTypedList
应该能够读取parcelablearlyist的列表,据我所知不会出错。
if (savedInstanceState != null) {
countryIndex = savedInstanceState.getInt(COUNTRIES_INDEX_KEY, -1);
countries = savedInstanceState
.getParcelableArrayList(COUNTRIES_KEY);
}
public Country(Parcel source) {
country.name = source.readString();
country.borders = source.readArrayList(Country.class
.getClassLoader());
}
@SuppressWarnings("rawtypes")
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public Country createFromParcel(Parcel in) {
return new Country(in);
}
public Country[] newArray(int size) {
return new Country[size];
}
};
public class ParcelableArrayList extends ArrayList<LatLng> implements
Parcelable {
public ParcelableArrayList(){
super();
}
protected ParcelableArrayList(Parcel in) {
in.readList(this, LatLng.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(this);
}
public static final Parcelable.Creator<ParcelableArrayList> CREATOR = new Parcelable.Creator<ParcelableArrayList>() {
public ParcelableArrayList createFromParcel(Parcel in) {
return new ParcelableArrayList(in);
}
public ParcelableArrayList[] newArray(int size) {
return new ParcelableArrayList[size];
}
};
}
public class Country implements Parcelable {
public static final Parcelable.Creator<Country> CREATOR = new Creator<Country>() {
@Override
public Country[] newArray(int size) {
return new Country[size];
}
@SuppressWarnings("unchecked")
@Override
public Country createFromParcel(Parcel source) {
return new Country(in);
}
};
public String name;
public List<ParcelableArrayList> borders;
public Country(Parcel source) {
country.name = source.readString();
country.borders = new ArrayList<ParcelableArrayList()>();
country.borders = source.readTypedList(ParcelableArrayList.CREATOR);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeList(borders);
}
}
country.borders = new ArrayList<ParcelableArrayList()>();
country.borders = source.readTypedList(ParcelableArrayList.CREATOR);
country.borders = new ArrayList<ParcelableArrayList()>();
country.borders = source.readList(country.borders, ParcelableArrayList.class.getClassLoader());
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class Lister extends ArrayList<AlertEvaluation> implements Parcelable {
protected Lister(Parcel in) {
this.addAll(in.readArrayList(AlertEvaluation.class.getClassLoader()));
}
public static final Creator<Lister> CREATOR = new Creator<Lister>() {
@Override
public Lister createFromParcel(Parcel in) {
return new Lister(in);
}
@Override
public Lister[] newArray(int size) {
return new Lister[size];
}
};
@Override public int describeContents() {
return 0;
}
@Override public void writeToParcel(Parcel parcel, int flags) {
parcel.writeList(this);
}
}