3

在构建使用 Room 库、用于依赖注入的 Dagger 和 Kotlin 的项目时,我遇到了这个错误:

[kapt] 发生异常:java.lang.IllegalArgumentException: voidcannot be convert to an Element

这是我的代码:

@Entity(tableName = "contacts_table")
data class Contact(@PrimaryKey var cid: Long,
          @ColumnInfo(name = "firstColor") var firstColor: String?,
          @ColumnInfo(name = "secondColor") var secondColor: String?,
          @ColumnInfo(name = "avatarUrl") var avatarUrl: String?,
          @ColumnInfo(name = "name") var name: String,
          @ColumnInfo(name = "nickname") var nickname: String)


@Dao
interface ContactsDao {

     @Insert(onConflict = REPLACE)
     fun insertContact(contact: Contact)

     @Query("delete from contacts_table where cid=:id")
     fun deleteContactById(id: Long)

     @Query("select * from contacts_table")
     fun getAllContacts() : List<Contact>
}


@Database(entities = [Contact::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
     abstract fun contactsDao()
}

这是我如何使用 DAO 的示例:

contactsDao.insertContact(Contact(response.id, response.firstColor, response.secondColor, 
        null, response.name, response.nickname)) 

contactsDao.insertContact(Contact(response.id, null, null, response.avatarUrl, 
        response.name, response.nickname))

如何修复此构建错误?

4

1 回答 1

4

在您的AppDatabase中,您必须为contactsDao(). 一定是ContactsDao。因此,您的课程应如下所示:

@Database(entities = [Contact::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
     abstract fun contactsDao(): ContactsDao
}
于 2018-08-12T14:10:48.940 回答