首页 > 解决方案 > Android Room 报错 ProductDatabase_Impl 不是抽象的,没有覆盖 ProductDatabase 中的抽象方法 getProductDao()

问题描述

我已经使用 Kotlin 在我的项目中实现了 RoomDatabase。我不断收到以下错误。

error: ProductDatabase_Impl is not abstract and does not override abstract method getProductDao() 
in ProductDatabase
public final class ProductDatabase_Impl extends ProductDatabase {

这是我的 Dao 接口和 ProductDatabase:

@Dao
interface ProductDao {

@Insert
suspend fun insertProduct(product: Product) : Long

@Insert
suspend fun insertAll(products: ArrayList<Product>) : List<Long>

@Update
suspend fun updateProduct(product: Product) : Int

@Query("SELECT * FROM product_table")
fun getAllProducts() : LiveData<List<Product>>
}


@Database(entities = [Product::class], version = 1)
abstract class ProductDatabase : RoomDatabase() {

abstract fun productDao(): ProductDao
abstract val productDao: ProductDao

companion object {..//Initialising..}

我在片段中使用了 dao:

val dao = ProductDatabase.getInstance(requireContext().applicationContext).productDao()
    repository = ProductRepository(dao)
    val factory = ProductViewModelFactory(repository)

我确实搜索过它,但我发现的主要是使用 kapt 而不是 annotationProcessor,但我从一开始就使用了 kapt,其他东西都不起作用。这是我的房间依赖项。

implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-ktx:$roomVersion"
androidTestImplementation "androidx.room:room-testing:$roomVersion"

标签: androidkotlinoverridingandroid-roomdao

解决方案


显然我创建了一个名为的包interface,它被检测为 Android Studio 中的一个目录。我将 dao 保存在那个包中,这就是编译器找不到 dao 接口的原因。通过从该目录中删除 dao 解决了错误。


推荐阅读