无法为类创建转换器[英] Unable to create converter for class

本文是小编为大家收集整理的关于无法为类创建转换器的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在我的应用程序中使用Retrofit和JSON的GSON转换器.没有Internet连接时,我想使用数据库.我决定使用糖ORM. 但是我得到了一个IllegalArgumentException.

java.lang.illegalargumentException:无法为类创建转换器

这是我的对象

public class User extends SugarRecord implements Parcelable {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("lastname")
    @Expose
    private String lastname;
    @SerializedName("properties")
    @Expose @Ignore
    private List<Object> properties = new ArrayList<>();
    @SerializedName("rights")
    @Expose @Ignore
    private String rights;
    @SerializedName("photo")
    @Expose
    private String photo;
    @SerializedName("favorites")
    @Expose @Ignore
    private List<PointsOnMap> favorites = new ArrayList<>();
    @SerializedName("token")
    @Expose
    private String token;

    public User() {
    }

    public User(String name, String lastname, String email, String photo, String token) {
        this.name = name;
        this.lastname = lastname;
        this.email = email;
        this.photo = photo;
        this.token = token;
    }
    //getters and setters

推荐答案

问题是,使用糖时,您不能在类中使用id的变量,因为默认情况下它使用了自己的内部ID.尝试重命名为userId或类似的东西.

其他推荐答案

只需在 threstient 语句整数 type.

    @SerializedName("id")
    @Expose
    private transient Integer id;

那么,如果您有 getID 和 setID 方法,则应删除它们.

玩得开心!

本文地址:https://www.itbaoku.cn/post/2091048.html

问题描述

I am using retrofit in my app and Gson convertor for JSON. I would like to use database when there is not internet connection. I decided to use Sugar ORM. but I get an IllegalArgumentException.

java.lang.IllegalArgumentException: Unable to create converter for class

here is my object

public class User extends SugarRecord implements Parcelable {

    @SerializedName("id")
    @Expose
    private Integer id;
    @SerializedName("email")
    @Expose
    private String email;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("lastname")
    @Expose
    private String lastname;
    @SerializedName("properties")
    @Expose @Ignore
    private List<Object> properties = new ArrayList<>();
    @SerializedName("rights")
    @Expose @Ignore
    private String rights;
    @SerializedName("photo")
    @Expose
    private String photo;
    @SerializedName("favorites")
    @Expose @Ignore
    private List<PointsOnMap> favorites = new ArrayList<>();
    @SerializedName("token")
    @Expose
    private String token;

    public User() {
    }

    public User(String name, String lastname, String email, String photo, String token) {
        this.name = name;
        this.lastname = lastname;
        this.email = email;
        this.photo = photo;
        this.token = token;
    }
    //getters and setters

推荐答案

Problem is you can't have a variable named id in your class when using Sugar since by default it uses its own internal ids. Try renaming to userId or something like that.

其他推荐答案

Just use transient statement before Integer type.

    @SerializedName("id")
    @Expose
    private transient Integer id;

Then, if you have getId and setId methods, should delete them.

And have fun!