我想使用@Parcel 注释而不是实现 Parcelable。
所以我的课是:
@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;
}
}
当我想构建项目时,这个错误会出现在 MessageBox
Error:(62, 109) error: incompatible types: Object cannot be converted to User
和用户源代码是:
@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() {
}
}
显然这个问题与列表有关。
我该如何解决我的问题?
谢谢。
EDIT1:我将用户类更改为以下代码:
@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() {
}
}
在 Parceler 库自动生成的UserSkillSpec$$ Parcelable 类中,错误发生在 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));
}
}