【问题标题】:Android Room error ProductDatabase_Impl is not abstract and does not override abstract method getProductDao() in ProductDatabaseAndroid Room 报错 ProductDatabase_Impl 不是抽象的,没有覆盖 ProductDatabase 中的抽象方法 getProductDao()
【发布时间】:2020-06-30 14:59:34
【问题描述】:

我在我的项目中使用 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,其他的东西都不起作用。这是我的 Room 依赖项。

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"

【问题讨论】:

    标签: android kotlin overriding android-room dao


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-20
      • 2014-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多