我正在使用Kotlin对项目进行编程:
我将Parceler与RealmObjects一起用于FragmentArgs,并遇到以下崩溃。
打包程序:无法找到类型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()
我的类(class)RealmString.kt像这样:
@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和实现= JobRealmProxy,仍然遇到相同的问题。
更新2
将 Realm版本更新为 3.5.0 ,仍然遇到相同的问题。
最佳答案
终于我得到了这个答案的解决方案。您只需要在模型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}
我的类(class)RealmString.kt像这样:
@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))
}
}