我使用Kotlin编写项目:
我正在使用Parceler将FragmentArgs与RealmObjects一起使用并导致以下崩溃 Parceler:无法找到类型为io.realm.RealmList的读/写生成器
@Parcel(value = Parcel.Serialization.BEAN, analyze = arrayOf(Job::class))
open class Job(
@PrimaryKey open var tweetId: Long? = -1L,
open var text: String = "",
open var title: String = "",
open var longitude: Double? = null,
open var latitude: Double? = null,
open var link: String = "",
@ParcelPropertyConverter(RealmStringListParcelConverter::class)
open var imageUrls: RealmList<RealmString>? = null
) : RealmObject()
@Parcel(value = Parcel.Serialization.BEAN, analyze = arrayOf(RealmString::class))
open class RealmString(
open var value: String = ""
) : RealmObject()
public class RealmListParcelConverter implements TypeRangeParcelConverter<RealmList<? extends RealmObject>, RealmList<? extends RealmObject>>
{
private static final int NULL = -1;
@Override
public void toParcel(RealmList<? extends RealmObject> input, Parcel parcel)
{
parcel.writeInt(input == null ? NULL : input.size());
if(input != null)
{
for(RealmObject item : input)
{
parcel.writeParcelable(Parcels.wrap(item), 0);
}
}
}
@Override
public RealmList fromParcel(Parcel parcel)
{
int size = parcel.readInt();
RealmList list = new RealmList();
for(int i = 0; i < size; i++)
{
Parcelable parcelable = parcel.readParcelable(getClass().getClassLoader());
list.add((RealmObject) Parcels.unwrap(parcelable));
}
return list;
}
}
我试过添加实现= RealmStringRealmProxy和implements = JobRealmProxy,仍然会遇到同样的问题。
将领域版本更新为 3.5.0 ,仍然会遇到同样的问题。
答案 0 :(得分:1)
最后我得到了一个解决方案。您只需在我的模型setter
中添加Job
即可。我会在这里给出我的全部代码:
@Parcel(value = Parcel.Serialization.BEAN, analyze = arrayOf(Job::class))
open class Job(
@PrimaryKey open var tweetId: Long? = -1L,
open var text: String = "",
open var title: String = "",
open var longitude: Double? = null,
open var latitude: Double? = null,
open var link: String = "",
) :RealmObject(){
@ParcelPropertyConverter(RealmStringListParcelConverter::class)
open var imageUrls: RealmList<RealmString>? = null set}
@Parcel(value = Parcel.Serialization.BEAN, analyze = arrayOf(RealmString::class))
open class RealmString(
open var value: String = ""
) : RealmObject()
class RealmStringListParcelConverter : RealmListParcelConverter<RealmString>() {
override fun itemToParcel(input: RealmString?, parcel: Parcel?) {
parcel?.writeParcelable(Parcels.wrap(RealmString::class.java, input), 0)
}
override fun itemFromParcel(parcel: Parcel?): RealmString {
return Parcels.unwrap(parcel?.readParcelable<Parcelable>(RealmString::class.java.classLoader))
}
}