android - 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"
解决方案
显然我创建了一个名为的包interface
,它被检测为 Android Studio 中的一个目录。我将 dao 保存在那个包中,这就是编译器找不到 dao 接口的原因。通过从该目录中删除 dao 解决了错误。
推荐阅读
- javascript - React 本机应用程序版本构建安装在模拟器上但不在设备上
- mysql - 我可以在任意数量的连接的顶层有一个 WHERE 条件吗?
- c++ - 添加 std::fstream 会引入链接器错误 error LNK2038: mismatch detected for 'RuntimeLibrary'
- android - Admob 未在 android pie 上显示横幅广告
- python - 每个子矩阵的python numpy pinv
- r - 使用 Google Core Reporting API 解决 500 服务器错误
- raku - 在 perl6 中运行命令,在 shell 中运行的命令在 perl6 中运行时会产生故障
- javascript - 嵌套在第三方组件中时,Field 的 Formik 包为空
- javascript - javascript 在页面加载后作为最后一个脚本运行
- visual-studio - 尽管有限制,但搜索栏和按钮在横向视图中放置不当