我正在尝试在Android应用程序中实现Room。我正在尝试从API获取数据并将其保存在本地数据库中。但是当我运行该应用程序时,发生了此错误
这是我的数据库类:
@Database(entities = [
(User::class)],
version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
@Volatile
private var INSTANCE: UserDatabase? = null
fun getDatabase(context: Context): UserDatabase {
if (INSTANCE == null){
synchronized(this){
INSTANCE = Room.databaseBuilder(
context.applicationContext,
UserDatabase::class.java,
"user_database")
.build()
}
}
return INSTANCE!!
}
}
}
这是我的存储库:class Repository(application: Application) {
private var mUserDao: UserDao
init {
val db = UserDatabase.getDatabase(application)
}
@WorkerThread
fun createUser(user: User) {
mUserDao.createUser(user)
}
@WorkerThread
fun deleteUser(id: Int) {
mUserDao.deleteUser(id)
}
@WorkerThread
fun getUser(id: Int) {
mUserDao.getUser(id)
}
这是我的用户@Dao
interface UserDao{
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun createUser(user: User)
@Delete
fun deleteUser(int: Int)
@Query("SELECT * FROM user WHERE user_id = :userID")
fun getUser(userID: Int): LiveData<User>
}
我的LogginViewModel:class LoginViewModel(application: Application) : AndroidViewModel(application) {
var repository: Repository = Repository(application)
fun createUser(user: User) = repository.createUser(user)
}
也发生此错误:最佳答案
问题出在您的`@Delete
@Delete
fun deleteUser(int: Int)
应该是
@Delete
fun deleteUser(user: User)
@Delete
方法的参数应该是一个实体或实体的数组,而不是Int
关于android - e:[kapt]发生异常:java.lang.IllegalArgumentException:无法将int转换为Element,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54145123/