2017-06-08 11 views
0

Parcelableを実装する代わりに@Parcelアノテーションを使用します。
だから私のクラスは次のとおりです。エラー:互換性のない型:オブジェクトをMyClassに変換できません

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

}  

私はプロジェクトをビルドしたい場合に、このエラーがメッセージボックスに

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: 私はコード以下のようにUserクラスを変更:

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)); 
     } 
    } 
+0

'@Parcel()' - それは '@ Parcel'であってはなりませんか?また、どのラインにエラーが表示されますか? –

+0

RealmObjectはパーセル可能ですか?そして、なぜこの問題がリストに関連していると思われますか?このエラーでさらに情報がありますか? – Timo

+0

@Timo、私はユーザをパーセルブルとして宣言する必要があります。私はintent.putExtraどこかでそれを渡すので。 – saeid

答えて

0

最後に、私は私の問題を解決し、それを共有することを決定することができ、カスタムクリエーターを実装するために検討すべきです。
ヒント: 1.リストはGenericでなければなりません。 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;}  

と、この1:

@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クラスです:

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

:Parcelerライブラリによって自動的に生成されたクラスUserSkillSpec $$ Parcelable

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

、エラーがセクションの中に起こりましたあなたの親クラスは分割可能ではないので、おそらくあなたのユーザのために拡張している実装を使うべきです。

Tips

あなたはlike this

+0

実装で拡張を置き換えましたが、エラーを解決できませんでした。 – saeid

+0

エラーの詳細を追加できますか? – Timo

+0

そして、あなたがあなたのオブジェクトをパースして元に戻す部分 – Timo

関連する問題