当前位置: 首页 > 知识库问答 >
问题:

找不到实现(文件室)

潘刚洁
2023-03-14

我对Room或idk有一个问题,问题出在哪里,我需要帮助找出问题出在哪里,我使用的是Hilt DI,当创建数据库实例时它崩溃了,这是我的代码

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.todo, PID: 1895
    java.lang.RuntimeException: cannot find implementation for com.example.todo.data.ToDoDatabase. ToDoDatabase_Impl does not exist
        at androidx.room.Room.getGeneratedImplementation(Room.java:97)
        at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1358)
        at com.example.todo.di.AppModule.provideToDoDatabase(AppModule.kt:27)
        at com.example.todo.di.AppModule_ProvideToDoDatabaseFactory.provideToDoDatabase(AppModule_ProvideToDoDatabaseFactory.java:33)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC.toDoDatabase(DaggerMainApplication_HiltComponents_SingletonC.java:69)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC.access$2000(DaggerMainApplication_HiltComponents_SingletonC.java:48)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$SwitchingProvider.get(DaggerMainApplication_HiltComponents_SingletonC.java:583)
        at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC.toDoDao(DaggerMainApplication_HiltComponents_SingletonC.java:73)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC.access$1900(DaggerMainApplication_HiltComponents_SingletonC.java:48)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$SwitchingProvider.get(DaggerMainApplication_HiltComponents_SingletonC.java:580)
        at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$ViewModelCImpl.toDoRepository(DaggerMainApplication_HiltComponents_SingletonC.java:448)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$ViewModelCImpl.toDoViewModel(DaggerMainApplication_HiltComponents_SingletonC.java:452)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$ViewModelCImpl.access$1700(DaggerMainApplication_HiltComponents_SingletonC.java:429)
        at com.example.todo.DaggerMainApplication_HiltComponents_SingletonC$ViewModelCImpl$SwitchingProvider.get(DaggerMainApplication_HiltComponents_SingletonC.java:487)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory$1.create(HiltViewModelFactory.java:100)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:69)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:171)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:44)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelLazy.kt:31)
        at com.example.todo.fragments.add.AddFragment.getViewModel(AddFragment.kt:20)
        at com.example.todo.fragments.add.AddFragment.insertDataToDo(AddFragment.kt:63)
        at com.example.todo.fragments.add.AddFragment.onOptionsItemSelected(AddFragment.kt:45)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:3122)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:3226)
        at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:3126)
        at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:3226)
        at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:416)
        at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:358)
        at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
        at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
        at androidx.appcompat.app.AppCompatDelegateImpl.onMenuItemSelected(AppCompatDelegateImpl.java:1179)
        at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
        at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
        at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975)
        at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:624)
        at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
        at android.view.View.performClick(View.java:7441)
        at android.view.View.performClickInternal(View.java:7418)
        at android.view.View.access$3700(View.java:835)
        at android.view.View$PerformClick.run(View.java:28676)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

建筑格拉德尔

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'androidx.navigation.safeargs.kotlin'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-parcelize'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.todo"
        minSdk 24
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    buildFeatures {
        dataBinding true
        viewBinding true
    }
}

dependencies {
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    def nav_version = "2.3.5"
    def room_version = "2.3.0"
    def lifecycle_version = "2.4.0"

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    testImplementation 'junit:junit:'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    //Navigation component
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    //Room
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // Lifecycle
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"

    //Kotlin coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'

    //RecyclerView
    implementation 'jp.wasabeef:recyclerview-animators:4.0.2'

    //Hilt DI
    implementation "com.google.dagger:hilt-android:2.38.1"
    kapt "com.google.dagger:hilt-compiler:2.38.1"

}

托多达塔。kt

@Entity(tableName = "todo_table")
@Parcelize
data class ToDoData(
    @PrimaryKey(autoGenerate = true)
    var id: Int,
    var title: String,
    var description: String,
    var priority: Priority
) : Parcelable

托多达贝斯。kt

@Database(entities = [ToDoData::class], version = 1, exportSchema = false)
@TypeConverters(Converter::class)
abstract class ToDoDatabase : RoomDatabase() {

    abstract fun toDoDao(): ToDoDao

}

ToDovidewModel。kt

@HiltViewModel
class ToDoViewModel @Inject constructor(
    private val repository: ToDoRepository
) : ViewModel() {

    private val getAllData: LiveData<List<ToDoData>> = repository.getAllData

    fun insertData(toDoData: ToDoData) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.insertData(toDoData)
        }
    }
}

应用模块。kt

@Module
@InstallIn(SingletonComponent::class)
object AppModule {

    @Singleton
    @Provides
    fun provideToDoDatabase(
        @ApplicationContext app: Context
    ) = Room.databaseBuilder(
        app,
        ToDoDatabase::class.java,
        "todo_database"
    )
        .fallbackToDestructiveMigration()
        .build()

    @Singleton
    @Provides
    fun provideTodoDao(db: ToDoDatabase) = db.toDoDao()

}

添加片段。kt

这是我调用的函数,用于从AddFraank插入数据

    private val viewModel: ToDoViewModel by viewModels()


private fun insertDataToDo() {
        val title = binding.titleEt.text.toString()
        val priority = binding.spinnerPriorities.selectedItem.toString()
        val description = binding.descriptionEt.text.toString()

        val validation = verifyDataFromUser(title, description)
        if (validation) {
            val data = ToDoData(
                0,
                title,
                description,
                parsePriority(priority)
            )
            viewModel.insertData(data)
            Toast.makeText(requireContext(), "Successfully Added!", Toast.LENGTH_SHORT).show()

            findNavController().navigate(R.id.action_addFragment_to_listFragment)
        } else
            Toast.makeText(requireContext(), "Please fill out all fields", Toast.LENGTH_SHORT)
                .show()


    }

    private fun verifyDataFromUser(title: String, description: String): Boolean {
        return if (TextUtils.isEmpty(title) || TextUtils.isEmpty(description)) {
            false
        } else !(title.isEmpty() || description.isEmpty())
    }

    private fun parsePriority(priority: String): Priority {
        return when (priority) {
            "High Priority" -> Priority.HIGH
            "Medium Priority" -> Priority.MEDIUM
            "Low Priority" -> Priority.LOW
            else -> Priority.LOW
        }
    }

我已经在这里看到了一些关于同一个问题的问题,但它对我不起作用,所以我想知道问题在哪里

共有1个答案

谈灵均
2023-03-14

我遇到了和你完全一样的问题,经过半天的尝试重写和删除代码片段,最后我的问题通过在build.gradle文件中将annotationProcess更改为kapt得到了解决。

// Hilt, Dependency injection
def hilt_version = "2.38.1"
implementation "com.google.dagger:hilt-android:$hilt_version"
annotationProcessor "com.google.dagger:hilt-compiler:$hilt_version"

// Room database
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"

更改和修复后:

// Hilt, Dependency injection
def hilt_version = "2.38.1"
implementation "com.google.dagger:hilt-android:$hilt_version"
kapt "com.google.dagger:hilt-compiler:$hilt_version"

// Room database
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"

考虑到控制台上打印的错误文本,我认为这根本不是问题!

 类似资料:
  • 使用最新的Springboot和MapStruct版本,并使用Maven构建,我试图实现官方MapStruct网站中给出的“从这里开始”示例 我的代码更简单: pom.xml 汽车JAVA 卡片to.java } 卡马珀。java接口 主要应用 所有代码均在本公开回购协议中:https://github.com/pgbonino/mappertest 运行时,我遇到以下错误: 我在官方的MapSt

  • 我知道这被问了好几次,但看起来我尝试的一切都不奏效。 我也是新手,如果我犯了任何错误,我很抱歉。 我尝试使用Gradle和eclipse构建java jar。 Gradle构建如下所示: 但我总是以: 在DefaultDependencyHandler类型的对象上找不到参数[org.springframework.boot: spring-boot-starter-data-jpa]org.gra

  • 问题内容: 我最近一直在开发一个程序,该程序可以从Java程序编译并运行C ++程序,我已经使一切基本正常(或至少据我所知),但是后来我注意到有些东西被打印到错误流中: 如您所见,如果我通过SSH而不是Java代码进行操作,它会起作用吗? Java代码: 感谢您的任何帮助,它已得到批准! 问题答案: 告诉你问题所在。 您的一级引号过多,因此您正在寻找而不是。 该的Runtime.exec文档说:

  • 在spring Java框架中,我使用context.xml文件创建bean,然后用加载它。 我的程序抛出异常。 文件存在。我使用Gradle进行依赖关系管理,并且我的src文件夹(文件所在的位置)在intellij中标记为resource。我知道由于某种原因,即使我的src文件夹应该包含在classpath变量中,但gradle run任务只是不能从这里抓取它。 当我使用时,所有的工作都很好,但

  • PhpStorm 2021.1.1 Mac M1 11.3 当我尝试使用或查找文件时,PhpStorm未能找到该文件。似乎正在尝试从目录,而不是在项目目录中搜索。 我试图< code >文件|无效缓存,运气不好。有人能帮忙吗?