Room-无法调用无参数构造函数

时间:2018-09-30 06:37:44

标签: android firebase android-room

我正在使用Room和Firebase数据库维护备份。尝试从数据库中加载所有项目时出现此错误。我有no-args构造函数,并且尝试了所有方法,还重命名了PodcastImage模型,但是没有运气

E/AndroidRuntime: FATAL EXCEPTION: pool-8-thread-1
              Process: com.hussain.podcastapp, PID: 17939
              java.lang.RuntimeException: Unable to invoke no-args constructor for android.os.Parcelable$Creator<com.hussain.podcastapp.model.PodcastImage>. Registering an InstanceCreator with Gson for this type may fix this problem.
                  at com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:228)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
                  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
                  at com.google.gson.Gson.fromJson(Gson.java:927)
                  at com.google.gson.Gson.fromJson(Gson.java:892)
                  at com.google.gson.Gson.fromJson(Gson.java:841)
                  at com.hussain.podcastapp.database.RoomTypeConverters.stringToSomeObjectList(RoomTypeConverters.java:24)
                  at com.hussain.podcastapp.database.EntryDao_Impl.loadAllPodcasts(EntryDao_Impl.java:218)
                  at com.hussain.podcastapp.ui.SubscribeActivity.lambda$setUI$3$SubscribeActivity(SubscribeActivity.java:102)
                  at com.hussain.podcastapp.ui.SubscribeActivity$$Lambda$1.run(Unknown Source)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                  at java.lang.Thread.run(Thread.java:762)
               Caused by: java.lang.UnsupportedOperationException: Interface can't be instantiated! Interface name: android.os.Parcelable$Creator
                  at com.google.gson.internal.UnsafeAllocator.assertInstantiable(UnsafeAllocator.java:117)
                  at com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:49)

这是我的 PodcastImage模型,该模型已经具有无参数构造函数。

public class PodcastImage implements Parcelable {

    @Exclude
    @Ignore
    @SuppressWarnings("unused")
    public final Parcelable.Creator<PodcastImage> CREATOR = new Parcelable.Creator<PodcastImage>() {
        @Override
        public PodcastImage createFromParcel(Parcel in) {
            return new PodcastImage(in);
        }

        @Override
        public PodcastImage[] newArray(int size) {
            return new PodcastImage[size];
        }
    };

    public PodcastImage() {
    }

    public String label;

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @Ignore
    protected PodcastImage(Parcel in) {
        label = in.readString();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(label);
    }
}

这是我的TypeConverter

public class RoomTypeConverters {

    @TypeConverter
    public static List<PodcastImage> stringToSomeObjectList(String data) {
        if (data == null) {
            return Collections.emptyList();
        }

        Type listType = new TypeToken<List<PodcastImage>>() {
        }.getType();

        return new Gson().fromJson(data, listType);
    }

    @TypeConverter
    public static String someObjectListToString(List<PodcastImage> someObjects) {
        Gson gson = new Gson();
        return gson.toJson(someObjects);
    }
}

主要模型类

  @Entity(tableName = "entry")
public class Entry implements Parcelable {

    @Exclude
    @SuppressWarnings("unused")
    public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator<Entry>() {
        @Override
        public Entry createFromParcel(Parcel in) {
            return new Entry(in);
        }

        @Override
        public Entry[] newArray(int size) {
            return new Entry[size];
        }
    };
    @NonNull
    @PrimaryKey
    @Embedded
    @SerializedName("id")
    public FeedID feedId;
    @Embedded
    @SerializedName("title")
    public Title EntryTitle;
    @SerializedName("im:image")
    public List<PodcastImage> image;
    @Embedded
    @SerializedName("summary")
    public Summary summary;

    public Entry() {
    }

    public FeedID getFeedId() {
        return feedId;
    }

    public void setFeedId(FeedID feedId) {
        this.feedId = feedId;
    }

    @Ignore
    protected Entry(Parcel in) {
        feedId = (FeedID) in.readValue(FeedID.class.getClassLoader());
        EntryTitle = (Title) in.readValue(Title.class.getClassLoader());
        if (in.readByte() == 0x01) {
            image = new ArrayList<>();
            in.readList(image, PodcastImage.class.getClassLoader());
        } else {
            image = null;
        }
        summary = (Summary) in.readValue(Summary.class.getClassLoader());
    }

    @Exclude
    public Title getEntryTitle() {
        return EntryTitle;
    }

    @Exclude
    public void setEntryTitle(Title entryTitle) {
        this.EntryTitle = entryTitle;
    }

    public List<PodcastImage> getImage() {
        return image;
    }

    public Summary getSummary() {
        return summary;
    }

    public void setSummary(Summary summary) {
        this.summary = summary;
    }

    public void setImage(List<PodcastImage> image) {
        this.image = image;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("feedId", feedId);
        result.put("Title", EntryTitle);
        result.put("image", image);
        result.put("summary", summary);
        return result;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(feedId);
        dest.writeValue(EntryTitle);
        if (image == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(image);
        }
        dest.writeValue(summary);
}

0 个答案:

没有答案