我正在尝试在我的 android 应用程序中实现 Room。我正在尝试从 API 获取数据并将它们保存在本地数据库中。但是当我运行应用程序时发生了这个错误
e: [kapt] 发生异常:java.lang.IllegalArgumentException: intcannot be convert to an Element
这是我的数据库类:
@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)
}
这是我的 userDao
@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>
}
我的登录视图模型:
class LoginViewModel(application: Application) : AndroidViewModel(application) {
var repository: Repository = Repository(application)
fun createUser(user: User) = repository.createUser(user)
}
也发生此错误:
org.gradle.api.tasks.TaskExecutionException:在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor 的任务 ':app:kaptDebugKotlin' <30 内部调用> 执行失败$Worker.run(ThreadPoolExecutor.java:617)<1 internal call> at java.lang.Thread.run(Thread.java:745) 原因:org.gradle.api.GradleException:编译错误。有关更多详细信息,请参阅 org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17 internal 的 org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16) 的日志来电> ...33 更多