error: incompatible types: Object cannot be converted to MyClass

error: incompatible types: Object cannot be converted to MyClass

我想使用@Parcel 注释而不是实现 Parcelable。
所以我的 class 是:

@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() {
    }
}    

显然这个问题与列表有关。
我该如何解决我的问题?
谢谢。

编辑1: 我将用户 class 更改为以下代码:

@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 库自动生成的 class UserSkillSpec$$Parcelable 中,错误发生在以下部分:

public static void write(UserSkillSpec userSkillSpec$, android.os.Parcel parcel$, int flags$[=14=], IdentityCollection identityMap$[=14=]) {
        int identity$[=14=] = identityMap$[=14=] .getKey(userSkillSpec$);
        if (identity$[=14=] != -1) {
            parcel$ .writeInt(identity$[=14=]);
        } else {
            parcel$ .writeInt(identityMap$[=14=] .put(userSkillSpec$));
            Skill$$Parcelable.write(InjectionUtil.getField(Skill.class, UserSkillSpec.class, userSkillSpec$, "skill"), parcel$, flags$[=14=], identityMap$[=14=]);
            parcel$ .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$, "proficiency"));
            if (InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$, "endorsers") == null) {
                parcel$ .writeInt(-1);
            } else {
                parcel$ .writeInt(InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$, "endorsers").size());
                for (User user$[=14=] : InjectionUtil.getField(List.class, UserSkillSpec.class, userSkillSpec$, "endorsers")) {
                    User$$Parcelable.write(user$[=14=], parcel$, flags$[=14=], identityMap$[=14=]);
                }
            }
            parcel$ .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$, "verified")? 1 : 0));
            parcel$ .writeInt(InjectionUtil.getField(int.class, UserSkillSpec.class, userSkillSpec$, "endorses"));
            parcel$ .writeString(InjectionUtil.getField(String.class, UserSkillSpec.class, userSkillSpec$, "TAG"));
            parcel$ .writeInt((InjectionUtil.getField(boolean.class, UserSkillSpec.class, userSkillSpec$, "endorsed")? 1 : 0));
        }
    }

您可能应该为您的用户使用 implements over extend,因为您的父 class 不可打包。

Tips

您应该考虑实施您的自定义创建器like this

终于解决了我的问题,决定分享一下。
尖端: 1. 列表必须是通用的。List<Item> 不是 List
2. 在 List<Item>

的声明中使用 @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() {
    }
}

这是 parcelConvertor class:

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()));
    }
}

如果你想使用 RealmModel 而不是 RealmObject,你可以按照这个来使用 Parceler 库。

例如 Class:

@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