简体   繁体   English

没有 @Provides-annotated 方法就无法提供。 刀柄

[英]cannot be provided without an @Provides-annotated method. hilt

Log日志

C:\Users\hasif\AndroidStudioProjects\SamplePhone\app\build\generated\hilt\component_sources\debug\com\sherhotel\samplephone\MyApplication_HiltComponents.java:127: error: [Dagger/MissingBinding] com.sherhotel.samplephone.mhilt.AppDatabase cannot be provided without an @Provides-annotated method.
      public abstract static class SingletonC implements MyApplication_GeneratedInjector,
                             ^
          com.sherhotel.samplephone.mhilt.AppDatabase is injected at
              com.sherhotel.samplephone.mhilt.MyModule.provideDocumentDao(db)
          com.sherhotel.samplephone.mhilt.DocumentDao is injected at
              com.sherhotel.samplephone.mhilt.Repository(dao)
          com.sherhotel.samplephone.mhilt.Repository is injected at
              com.sherhotel.samplephone.mhilt.MyViewModel(db)
          com.sherhotel.samplephone.mhilt.MyViewModel is injected at
              com.sherhotel.samplephone.mhilt.MyViewModel_HiltModules.BindsModule.binds(vm)
          @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
              dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.sherhotel.samplephone.MyApplication_HiltComponents.SingletonC ? com.sherhotel.samplephone.MyApplication_HiltComponents.ActivityRetainedC ? com.sherhotel.samplephone.MyApplication_HiltComponents.ViewModelC]

gradle dep gradle 部门

 // Dagger-Hilt
    implementation "com.google.dagger:hilt-android:2.43.2"
    kapt "com.google.dagger:hilt-compiler:2.43.2"
    implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'
    // Room
    kapt "androidx.room:room-compiler:$room_version"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    // ViewModel Compose
    implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.1"

Code代码

    @Module
    @InstallIn(SingletonComponent::class)
    object MyModule {
    
        @Provides
        @Singleton
        fun provideDatabase(@ApplicationContext appContext: Context): RoomDatabase {
            return Room.databaseBuilder(
                appContext,
                AppDatabase::class.java,
                "wise_pdf_db"
            ).fallbackToDestructiveMigration()
                .build()
        }
    
        @Provides
        @Singleton
        fun provideDocumentDao(db: AppDatabase): DocumentDao =
            db.documentDao()
    
    
    }
    
    
    @Database(entities = [Document::class], version = 2, exportSchema = false)
    public abstract class AppDatabase : RoomDatabase() {
    
        abstract fun documentDao(): DocumentDao
    }
    
    @Dao
    interface DocumentDao {
    
        @Query("SELECT * FROM my_pdf_docs ORDER BY lastRead DESC")
        fun getRecentDocs(): LiveData<List<Document>>
    
        @Insert(onConflict = OnConflictStrategy.IGNORE)
        suspend fun insert(document: Document)
    
        @Query("DELETE FROM my_pdf_docs WHERE id = :id")
        suspend fun delete(id: Int)
    }
    
    
    @Entity(tableName = "my_pdf_docs", indices = [Index(value = ["filePath"], unique = true)])
    data class Document(
        @PrimaryKey(autoGenerate = true) val id: Int,
        @ColumnInfo(name = "filePath") val filePath: String,
        @ColumnInfo(name = "lastRead") val lastRead: Long
    )

@HiltViewModel
class MyViewModel @Inject constructor(private val db: Repository) : ViewModel() {
//    fun getData(): String {
//        return "123"
//    }
    suspend fun insert(document: Document) {
        db.insert(document)
    }
    val recentDocuments: LiveData<List<Document>> = db.getRecentDocs()
}

class Repository @Inject constructor(private val dao: DocumentDao) {
    suspend fun insert(document: Document) {
        dao.insert(document)
    }

    fun getRecentDocs(): LiveData<List<Document>> {
        return dao.getRecentDocs()
    }
    // class methods here
}

@HiltAndroidApp
class MyApplication : Application()



@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    val viewModel: MyViewModel by viewModels()

what i missing here?.我在这里错过了什么?

Change the return type of fun provideDatabase to AppDatabasefun provideDatabase的返回类型更改为AppDatabase

fun provideDatabase(@ApplicationContext appContext: Context): AppDatabase

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 如果没有 @Provides 注释的方法,则无法提供刀柄。 在存储库接口 Class - Android Hilt cannot be provided without an @Provides-annotated method. in repository Interface Class 匕首柄:如果没有@Provides-annotated 方法,则无法提供 - Dagger Hilt: cannot be provided without an @Provides-annotated method Dagger Hilt:如果没有 @Provides-annotated 方法,则无法提供存储库 - Dagger Hilt: Repository cannot be provided without an @Provides-annotated method 如果没有@Provides 注释的方法,则无法提供刀柄 - Hilt cannot be provided without an @Provides-annotated method 如果没有 @provides-annotated 方法就无法提供 - Dagger/Hilt - Cannot be provided without an @provides-annotated method - Dagger/Hilt 没有 @Provides-annotated 方法无法提供匕首柄 - Dagger hilt Cannot be provided without an @Provides-annotated method 没有@Provides注释的方法无法提供 - Cannot be Provided Without an @Provides-annotated Method Dagger2问题“如果没有@ Provide-annotated方法,则无法提供”。 - Dagger2 issue with “cannot be provided without an @Provides-annotated method.” Kotlin + 刀柄:Class 在注入 object 时,如果没有 @Provides 注释的方法,则无法提供 - Kotlin + Hilt: Class cannot be provided without an @Provides-annotated method when injecting into an object Hilt - 如果没有 @Inject 构造函数或 @Provides 注释方法,则无法提供应用程序 - Hilt - app cannot be provided without an @Inject constructor or an @Provides-annotated method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM