我收到此错误,我想我明白它的来源,但我不明白问题所在。我收到的错误的详细摘要。
> Task :app:kaptDebugKotlin
C:\Users\matan\AndroidStudioProjects\RoomTut\app\build\tmp\kapt3\stubs\debug\com\example\roomtut\data\MarkerDao.java:13: error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> p1);
^C:\Users\matan\AndroidStudioProjects\RoomTut\app\build\tmp\kapt3\stubs\debug\com\example\roomtut\data\MarkerDao.java:11: error: Not sure how to handle insert method's return type.
public abstract java.lang.Object addMarker(@org.jetbrains.annotations.NotNull()
^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).
> Task :app:kaptDebugKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 32s
29 actionable tasks: 9 executed, 20 up-to-date
我知道我的标记 dao 中存在问题,但对我而言,注释看起来不错,您可以在下面看到,还有指向我的完整项目的存储库的链接 - https://github.com/M-J-Y-21/room-test
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
@Dao
interface MarkerDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker)
@Query("SELECT * FROM marker_table ORDER BY id ASC")
fun readAllData(): LiveData<List<Marker>>
}
您可以在下面看到我是如何注释标记类的
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "marker_table") data class Marker(
@PrimaryKey(autoGenerate = true)
val id: Int,
val title: String,
val location: String,
val colour: String
)
下面是我的数据库类
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
@Database(entities = arrayOf(Marker::class), version = 1, exportSchema = false)
abstract class MarkerDatabase: RoomDatabase() {
abstract fun markerDao(): MarkerDao
companion object {
@Volatile
private var INSTANCE: MarkerDatabase? = null
fun getDatabase(context: Context): MarkerDatabase{
val tempInstance = INSTANCE
if (tempInstance != null) {
return tempInstance
}
synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
MarkerDatabase::class.java,
"marker_database"
).build()
INSTANCE = instance
return instance
}
}
}
}
答案 0 :(得分:0)
正如我所见,您的项目使用了不同版本的 androidx.room
依赖项,例如:
"androidx.room:room-runtime:2.3.0"
"androidx.room:room-compiler:2.2.5"
您应该为所有 androidx.room
的工件使用类似的版本。 Please look at sample with more right approach:
dependencies {
...
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
...
}
当我将 androidx.room
的所有版本更改为 2.3.0
时,一切正常。并且不要忘记更改 addMarker
的返回类型:
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker) // OK
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker): Long // OK
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addMarker(marker: Marker): Int // ERROR
我是如何寻找问题的:
@Insert
方法的返回类型(见上文)。@Entity
)。附言您通过 MarkerDatabase.Companion.getDatabase()
实现的单例是错误的 :( 请使用双重检查锁定算法,或将所有方法设为同步或尝试根据您的情况调整 kotlin 的 lazy
。