我正在尝试使用谷歌的Android体系结构组件中的新Room ORM,但我遵循了文档,但是当我运行代码时,我得到java.lang.RuntimeException: cannot find implementation for com.zeyad.usecases.app.UserDatabase. UserDatabase_Impl does not exist
这是我的代码:

Gradle项目:

allprojects {
repositories {
    jcenter()
    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.google.com' }
}
}

摇篮模块:
compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"

用户:
@Entity(tableName = "User")
public class User {

    static final String LOGIN = "login";
    private static final String ID = "id", AVATAR_URL = "avatar_url";
    @PrimaryKey
    @SerializedName(LOGIN)
    @ColumnInfo(name = LOGIN)
    String login;

    @SerializedName(ID)
    @ColumnInfo(name = ID)
    int id;

    @SerializedName(AVATAR_URL)
    @ColumnInfo(name = AVATAR_URL)
    String avatarUrl;

    public User() {
    }

    public User(String login, int id, String avatarUrl) {
        this.login = login;
        this.id = id;
        this.avatarUrl = avatarUrl;
    }

    public static boolean isEmpty(User user) {
        return user == null || (user.login == null && user.avatarUrl == null);
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        return id == user.id && (login != null ? login.equals(user.login) : user.login == null
                && (avatarUrl != null ? avatarUrl.equals(user.avatarUrl) : user.avatarUrl == null));
    }

    @Override
    public int hashCode() {
        int result = login != null ? login.hashCode() : 0;
        result = 31 * result + id;
        result = 31 * result + (avatarUrl != null ? avatarUrl.hashCode() : 0);
        return result;
    }
}

UserDatabase:
@Database(entities = {User.class}, version = 1)
public abstract class UserDatabase extends RoomDatabase {
    public abstract RoomBaseDao roomBaseDao();
}

RoomBaseDao:
@Dao
public interface RoomBaseDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertItemsReplace(User... objects);

    @Update(onConflict = OnConflictStrategy.REPLACE)
    void updateItemsReplace(User... objects);

    @Delete
    void deleteItem(User object);
}

应用程序onCreate:
UserDatabase gDb = Room.databaseBuilder(this, UserDatabase.class, "test-db").build(); // throws the exception above

提前致谢。

最佳答案

就我而言,我使用的是旧版本的 ButterKnife ,该版本需要android-apt插件。因此,我更新了 ButterKnife (现在使用注释处理器),并删除了 Gradle 文件中的插件声明。

可能这不是您的问题,但这也许会帮助其他人。

10-04 23:44