简体   繁体   中英

How to fix "Entities and Pojos must have a usable public constructor" error?

I'm trying to setup Room in my Android application. I'm getting this error but I really don't understand why.

Full error : Entities and Pojos must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).

I'm using version 1.1.1 of Room. I tried empty constructors, constructors with params and also fixed all the warnings that I had so that nothing else could be the problem.

Here are my entities :

Marker Entity

@Entity(tableName = "markers")
public class Marker {
    public Marker(@Nullable String title, @Nullable String snippet, String latitude, String longitude, float color) {
        this.title = title;
        this.snippet = snippet;
        this.latitude = latitude;
        this.longitude = longitude;
        this.color = color;
    }

    @PrimaryKey(autoGenerate = true)
    private int id;
    private String title;
    private String snippet;
    private String latitude;
    private String longitude;
    private float color;

    /* Getters and setters */
}

Photo Entity

@Entity(tableName = "photos",
        foreignKeys = @ForeignKey(entity = Marker.class,
                                  parentColumns = "id",
                                  childColumns = "marker_id"),
        indices = {@Index("marker_id")})
public class Photo {
    public Photo(String imageBase64, int markerId) {
        this.imageBase64 = imageBase64;
        this.markerId = markerId;
    }

    @PrimaryKey(autoGenerate = true)
    private int id;

    @ColumnInfo(name = "image")
    private String imageBase64;

    @ColumnInfo(name = "marker_id")
    private int markerId;

    /* Getters and setters */
}

Network Entity

@Entity(tableName = "networks",
        foreignKeys = @ForeignKey(entity = Marker.class,
                                  parentColumns = "id",
                                  childColumns = "marker_id"),
        indices = {@Index("marker_id")})
public class Network {
    public Network(String ssid, String bssid, String capabilities, long timestamp, int markerId) {
        this.ssid = ssid;
        this.bssid = bssid;
        this.capabilities = capabilities;
        this.timestamp = timestamp;
        this.markerId = markerId;
    }

    @PrimaryKey(autoGenerate = true)
    private int id;

    private String ssid;
    private String bssid;
    private String capabilities;
    private long timestamp;

    @ColumnInfo(name = "marker_id")
    private int markerId;

    /* Getters and setters */
}

And here is my dependencies for Room in my build.gradle file :

def room_version = "1.1.1"

implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version"

I looked at many questions like this and none of them solved my problem.

Create empty constructors like this.

public Marker(){};

public Photo(){};

public Network(){};

or you could remove all the constructors. Clean the project and rebuild. Check if this works.

The error was due to a mistake in the MarkerDao class and not the Entity itself (somehow).

More precisely, a function was trying to get a LatLng object from a SELECT string, string from ... query:

@Query("SELECT latitude, longitude FROM markers WHERE id = :id")
LatLng getMarkerLatitude(int id);

This error was very annoying to debug as it gives no details. If someone finds this, comment out your entities and DAOs and uncomment them one at a time to see where the error starts happening.

Good luck!

Source: I work with @Jean-Christophe Martel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM