e: [kapt] 发生异常:java.lang.IllegalArgumentException: intcannot be converted to an Element
e: [kapt] An exception occurred: java.lang.IllegalArgumentException: intcannot be converted to an Element
我正在尝试在我的 android 应用程序中实施 Room。我正在尝试从 API 获取数据并将它们保存在本地数据库中。但是当我运行应用程序出现这个错误
e: [kapt] An exception occurred: java.lang.IllegalArgumentException:
intcannot be converted to an Element
这是我的数据库class:
@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>
}
我的 LogginViewModel:
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> at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<1
internal call> at java.lang.Thread.run(Thread.java:745) Caused by:
org.gradle.api.GradleException: Compilation error. See log for more
details at
org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16)
at
org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17
internal calls> ...33 more
问题出在你的@Delete
@Delete
fun deleteUser(int: Int)
应该是
@Delete
fun deleteUser(user: User)
@Delete
方法的参数应该是实体或实体数组,而不是 Int
这是解决方案:
正如@musooff 上面提到的,我改变了这样的删除方法:
@Delete
fun deleteUser(user: User)
或者如果您想删除具有特定 ID 的用户,请执行此查询:
@Query("DELETE FROM user WHERE user_id = :id")
fun deleteUser(id: Int)
然后要解决下一个错误,您应该在 Asynctask 中执行 UserDao 中的方法
例如在存储库文件中创建用户我这样做了:
private class insertAsyncTask internal constructor(private val mAsyncTaskDao: UserDao) : AsyncTask<User, Void, Void>() {
override fun doInBackground(vararg params: User): Void? {
mAsyncTaskDao.createUser(params[0])
return null
}
}
然后 Repository 中的 createUser 函数如下所示:
fun createUser(user: User) {
insertAsyncTask(mUserDao).execute(user)
}
这解决了我的问题。
我正在尝试在我的 android 应用程序中实施 Room。我正在尝试从 API 获取数据并将它们保存在本地数据库中。但是当我运行应用程序出现这个错误
e: [kapt] An exception occurred: java.lang.IllegalArgumentException: intcannot be converted to an Element
这是我的数据库class:
@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>
}
我的 LogginViewModel:
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> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)<1 internal call> at java.lang.Thread.run(Thread.java:745) Caused by: org.gradle.api.GradleException: Compilation error. See log for more details at org.jetbrains.kotlin.gradle.tasks.TasksUtilsKt.throwGradleExceptionIfError(tasksUtils.kt:16) at org.jetbrains.kotlin.gradle.internal.KaptWithKotlincTask.compile(KaptWithKotlincTask.kt:79)<17 internal calls> ...33 more
问题出在你的@Delete
@Delete
fun deleteUser(int: Int)
应该是
@Delete
fun deleteUser(user: User)
@Delete
方法的参数应该是实体或实体数组,而不是 Int
这是解决方案:
正如@musooff 上面提到的,我改变了这样的删除方法:
@Delete
fun deleteUser(user: User)
或者如果您想删除具有特定 ID 的用户,请执行此查询:
@Query("DELETE FROM user WHERE user_id = :id")
fun deleteUser(id: Int)
然后要解决下一个错误,您应该在 Asynctask 中执行 UserDao 中的方法 例如在存储库文件中创建用户我这样做了:
private class insertAsyncTask internal constructor(private val mAsyncTaskDao: UserDao) : AsyncTask<User, Void, Void>() {
override fun doInBackground(vararg params: User): Void? {
mAsyncTaskDao.createUser(params[0])
return null
}
}
然后 Repository 中的 createUser 函数如下所示:
fun createUser(user: User) {
insertAsyncTask(mUserDao).execute(user)
}
这解决了我的问题。