android 错误:[Dagger/MissingBinding],如果没有@ Provides注解的方法,则无法提供

zvms9eto  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(191)

我尝试使用Hilt将天气存储库注入到我的视图模型中,但得到以下错误消息:
WeatherRepository不能在没有@ Provides注解的方法的情况下提供。
公共抽象静态类SingletonC实现WeatherApplication_GeneratedInjector

Weather DAO:

  1. @Dao
  2. interface WeatherDao {
  3. @Insert(onConflict = OnConflictStrategy.REPLACE)
  4. suspend fun insertWeatherItem(weatherItem: WeatherItem)
  5. @Query("DELETE FROM WEATHERITEM WHERE cityName = :cityName")
  6. suspend fun deleteWeatherItem(cityName: String)
  7. @Query("SELECT * FROM WEATHERITEM WHERE cityId = :id")
  8. fun getWeatherItem(id: Int): Flow<WeatherItem>
  9. @Query("SELECT * FROM WEATHERITEM")
  10. fun getAllWeatherItems(): Flow<List<WeatherItem>>
  11. }

字符串

天气仓库:

  1. class WeatherRepository(private val weatherDao: WeatherDao) {
  2. suspend fun insertWeatherItem(weatherItem: WeatherItem) = weatherDao.insertWeatherItem(weatherItem)
  3. suspend fun deleteWeatherItem(cityName: String) = weatherDao.deleteWeatherItem(cityName)
  4. fun getWeatherItem(id: Int): Flow<WeatherItem> = weatherDao.getWeatherItem(id)
  5. fun getAllWeatherItems(): Flow<List<WeatherItem>> = weatherDao.getAllWeatherItems()
  6. }

天气数据库:

  1. @Database(entities = [WeatherItem::class] ,version = 1)
  2. abstract class WeatherDatabase :RoomDatabase(){
  3. abstract fun weatherDao(): WeatherDao
  4. }

天气模块:

  1. @Module
  2. @InstallIn(ActivityComponent::class)
  3. object WeatherModule {
  4. @Provides
  5. @Singleton
  6. fun provideDatabase(@ApplicationContext context: Context): WeatherDatabase {
  7. return Room.databaseBuilder(
  8. context,
  9. WeatherDatabase::class.java,
  10. "WeatherDatabase")
  11. .fallbackToDestructiveMigration()
  12. .build()
  13. }
  14. @Provides
  15. @Singleton
  16. fun provideWeatherDao(weatherDatabase: WeatherDatabase): WeatherDao {
  17. return weatherDatabase.weatherDao()
  18. }
  19. @Provides
  20. @Singleton
  21. fun provideWeatherRepository(weatherDao: WeatherDao): WeatherRepository {
  22. return WeatherRepository(weatherDao)
  23. }
  24. }

天气视图模型:

  1. @HiltViewModel
  2. class WeatherViewModel @Inject constructor(
  3. private val weatherRepository: WeatherRepository,
  4. ) : ViewModel()

主要活动:

  1. @AndroidEntryPoint
  2. class MainActivity : ComponentActivity() {
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. val viewModel: WeatherViewModel by viewModels()
  6. setContent {
  7. WindSpellTheme(darkTheme = darkTheme) {
  8. Surface(
  9. modifier = Modifier.fillMaxSize(),
  10. color = MaterialTheme.colorScheme.background
  11. ) {
  12. MainScreen(viewModel) {
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

适用范围:

  1. @HiltAndroidApp
  2. class WeatherApplication: Application()


版本2.47

备注

当导航到 Find usages 时,显示存在对注入对象的引用:x1c 0d1x

dl5txlt9

dl5txlt91#

请尝试从class WeatherRepository @Inject constructor(private val weatherDao: WeatherDao)中删除@Inject constructor,因为您已经在WeatherModule中拥有它。
现在它应该工作。

**P.S.**如果你以后决定让WeatherRepository成为一个接口,比如WeatherRepositoryImpl,你可以这样做:

  1. @Singleton
  2. class WeatherRepositoryImpl @Inject constructor(private val weatherDao: WeatherDao): WeatherRepository {
  3. //implementation
  4. }

字符串
然后,在一些Module中,它是一个 * 接口 * 或 * 抽象类 *:

  1. @Binds
  2. @Singleton
  3. fun bindWeatherRepository(impl: WeatherRepositoryImpl): WeatherRepository

展开查看全部

相关问题