Android 错误:不兼容的类型:无法将对象转换为MyClass
我想使用@Parcel annotation而不是实现Parcelable。Android 错误:不兼容的类型:无法将对象转换为MyClass,android,parceler,Android,Parceler,我想使用@Parcel annotation而不是实现Parcelable。 所以我的课是: @Parcel public class UserSkillSpec { private final String TAG = getClass().getSimpleName(); private Skill skill; private int endorses = 0; private boolean endorsed = false; private L
所以我的课是:
@Parcel
public class UserSkillSpec {
private final String TAG = getClass().getSimpleName();
private Skill skill;
private int endorses = 0;
private boolean endorsed = false;
private List<User> endorsers = new LinkedList<>();
private int proficiency = 0;
private boolean verified = false;
@ParcelConstructor
public UserSkillSpec(Skill skill, int proficiency) {
this.skill = skill;
this.proficiency = proficiency;
}
}
用户源代码为:
@Parcel
@RealmClass
public class User extends RealmObject {
@PrimaryKey
private String id;
private String firstName;
private String lastName;
private String name;
//and something else
public User() {
}
}
显然,这个问题与列表有关。如何解决我的问题?
谢谢 编辑1: 我将用户类更改为以下代码:
@Parcel(implementations = {UserRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {User.class})
@RealmClass
public class User implements RealmModel {
@PrimaryKey
private String id;
private String firstName;
private String lastName;
private String name;
//somethings else
public User() {
}
}
在自动生成的类UserSkillSpec$$Parcelable by Parceler library中,for部分出错:
public static void write(UserSkillSpec userSkillSpec$$1, android.os.Parcel parcel$$1, int flags$$0, IdentityCollection identityMap$$0) {
int identity$$0 = identityMap$$0 .getKey(userSkillSpec$$1);
if (identity$$0 != -1) {
parcel$$1 .writeInt(identity$$0);
} else {
parcel$$1 .writeInt(identityMap$$0 .put(userSkillSpec$$1));
Skill$$Parcelable.write(InjectionUtil.getField(Skill.class, UserSkillSpec.class, userSkillSpec$$1, "skill"), parcel$$1, flags$$0, identityMap$$0);
parcel$$1 .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$$1, "proficiency"));
if (InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers") == null) {
parcel$$1 .writeInt(-1);
} else {
parcel$$1 .writeInt(InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers").size());
for (User user$$0 : InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$$1, "endorsers")) {
User$$Parcelable.write(user$$0, parcel$$1, flags$$0, identityMap$$0);
}
}
parcel$$1 .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$$1, "verified")? 1 : 0));
parcel$$1 .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$$1, "endorses"));
parcel$$1 .writeString(InjectionUtil.getField(String.class, UserSkillSpec.class, userSkillSpec$$1, "TAG"));
parcel$$1 .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$$1, "endorsed")? 1 : 0));
}
}
您可能应该为您的用户使用implements over extend,因为您的父类是不可打包的
<>你应该考虑实现你的自定义创建者 最后我可以解决我的问题,并决定分享它。 提示: 1.列表必须是通用的。
列表
不是列表
2.在
列表的声明中使用@ParcelPropertyConverter(ItemListParcelConverter.class)
因此,我的代码如下所示:
@Parcel(implementations = {UserRealmProxy.class},
value = Parcel.Serialization.BEAN,
analyze = {User.class})
public class User extends RealmObject {
@PrimaryKey
private String id;
private String firstName;
private String lastName;
private String name;}
还有这个:
@Parcel
public class UserSkillSpec {
private Skill skill;
private int endorses = 0;
private boolean endorsed = false;
@ParcelPropertyConverter(ItemListParcelConverter.class)
private List<User> endorsers = new LinkedList<>();
private int proficiency = 0;
private boolean verified = false;
public UserSkillSpec() {
}
}
@包裹
公共类UserSkillSpec{
个人技能;
私有int=0;
私有布尔值=false;
@ParcelPropertyConverter(ItemListParcelConverter.class)
私有列表背书人=新建LinkedList();
私人智力熟练程度=0;
私有布尔验证=假;
公共UserSkillSpec(){
}
}
这是parcelConvertor类:
public class ItemListParcelConverter<T> extends ArrayListParcelConverter<T> {
@Override
public void itemToParcel(T item, Parcel parcel) {
parcel.writeParcelable(Parcels.wrap(item), 0);
}
@Override
public T itemFromParcel(Parcel parcel) {
return Parcels.unwrap(parcel.readParcelable(getClass().getClassLoader()));
}
}
公共类ItemListParcelConverter扩展了ArrayListParcelConverter{
@凌驾
公共无效项目托帕尔(T项目、包裹){
包裹。可写可塞(包裹包裹包裹(项目),0);
}
@凌驾
公共T项目从包裹(包裹){
返回包裹。展开(包裹。可读取包裹(getClass().getClassLoader());
}
}
如果您想使用RealmModel而不是RealmObject,您可以按照此操作使用Parceler库
舱外:
@Parcel(implementations = { CustomAreaRealmProxy.class },
value = Parcel.Serialization.BEAN,
analyze = { CustomArea.class })
@RealmClass
public class CustomArea implements RealmModel {
...
}
和你的proguard文件
# Parceler library
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
-keepnames public class * extends io.realm.RealmObject
-keepnames public class * implements io.realm.RealmModel #<- This is the fix
#包裹器库
-保留接口org.parceler.Parcel
-keep@org.parceler.Parcel类*{*;}
-保持类**$$Parcelable{*;}
-keepnames公共类*扩展io.realm.RealmObject
-keepnames公共类*实现io.realm.RealmModel#@Parcel()
-它不应该是@Parcel
?错误出现在哪一行?RealmObject是可包裹的?为什么你认为这个问题与列表有关?你有关于这个错误的更多信息吗?@Timo,我需要将用户声明为Parcelable。因为我通过intent.putExtra在某处传递它。是的,但是如果您扩展或您的父类需要是可包裹的,则不能传递到错误所在的行?你能在stacktrace上粘贴更多吗?我用实现替换了extends,但我无法解决错误。还有你将对象打包并取回的部分
# Parceler library
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
-keepnames public class * extends io.realm.RealmObject
-keepnames public class * implements io.realm.RealmModel #<- This is the fix