gpt4 book ai didi

android - e : [kapt] An exception occurred: java. lang.IllegalArgumentException:intcannot be converted to an Element

转载 作者:行者123 更新时间:2023-12-01 23:09:39 24 4
gpt4 key购买 nike

我正在尝试在我的 android 应用程序中实现 Room。我正在尝试从 API 获取数据并将它们保存在本地数据库中。但是当我运行应用程序时出现了这个错误

e: [kapt] An exception occurred: java.lang.IllegalArgumentException:intcannot be converted 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>

}

我的登录 View 模型:

class LoginViewModel(application: Application) : AndroidViewModel(application) {

var repository: Repository = Repository(application)


fun createUser(user: User) = repository.createUser(user)

}

也发生了这个错误:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task':app:kaptDebugKotlin' <30 internal calls> atjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<1internal call> at java.lang.Thread.run(Thread.java:745) Caused by:org.gradle.api.GradleException: Compilation error. See log for moredetails atorg.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16)atorg.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17internal calls> ...33 more

最佳答案

问题出在你的@Delete

@Delete
fun deleteUser(int: Int)

应该是什么

@Delete
fun deleteUser(user: User)

@Delete 方法的参数应该是实体或实体数组,而不是Int

关于android - e : [kapt] An exception occurred: java. lang.IllegalArgumentException:intcannot be converted to an Element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54145123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com