kotlin MainActivity不实现接口dagger.hilt.internal.GeneratedComponent或接口dagger.hilt.internal. GeneratedComponent Manager

hpxqektj  于 2023-11-21  发布在  Kotlin
关注(0)|答案(2)|浏览(381)

我正在学习使用Jetpack Compose,我试图按照教程使用Hilt和MVVM创建一个Pokedex,一切都很顺利,直到我试图实现viewModel,当我注入它并试图运行应用程序时,我得到了标题错误(Given component holder class com.example.pokedex.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager),我已经尝试了几个解决方案,从论坛改变注入方式,但没有一个对我有效。
我的代码:

Pokedex应用程序:

  1. @HiltAndroidApp
  2. class PokedexApplication : Application() {
  3. override fun onCreate() {
  4. super.onCreate()
  5. Timber.plant(Timber.DebugTree())
  6. }
  7. }

字符串

主活动

  1. @AndroidEntryPoint
  2. class MainActivity : ComponentActivity() {
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. setContent {
  6. PokedexTheme {
  7. val navController = rememberNavController()
  8. NavHost(navController = navController,
  9. startDestination = "pokemon_list_screen"
  10. ) {
  11. composable("pokemon_list_screen") {
  12. PokemonListScreen(navController = navController)
  13. }
  14. composable(
  15. "pokemon_detail_screen/{dominantColor}/{pokemonName}",
  16. arguments = listOf(
  17. navArgument("dominantColor") {
  18. type = NavType.IntType
  19. },
  20. navArgument("pokemonName") {
  21. type = NavType.StringType
  22. }
  23. )) {
  24. val dominantColor = remember {
  25. val color = it.arguments?.getInt("dominantColor")
  26. color?.let { Color(it) } ?: Color.White
  27. }
  28. val pokemonName = remember {
  29. it.arguments?.getString("pokemonName")
  30. }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }

AppModule

  1. @Module
  2. @InstallIn(SingletonComponent::class)
  3. object AppModule {
  4. @Singleton
  5. @Provides
  6. fun providePokemonRepository(
  7. api: PokeApi
  8. ) = PokemonRepository(api)
  9. @Singleton
  10. @Provides
  11. fun providePokeApi(): PokeApi {
  12. return Retrofit.Builder()
  13. .addConverterFactory(GsonConverterFactory.create())
  14. .baseUrl(BASE_URL)
  15. .build()
  16. .create(PokeApi::class.java)
  17. }
  18. }

仓库

  1. @ActivityScoped
  2. class PokemonRepository @Inject constructor (
  3. private val api: PokeApi
  4. ) {
  5. suspend fun getPokemonList(limit: Int, offset: Int): Resource<PokemonList> {
  6. val response = try {
  7. api.getPokemonList(limit, offset)
  8. } catch (e: Exception) {
  9. return Resource.Error(e.message.toString())
  10. }
  11. return Resource.Success(response)
  12. }
  13. }

可组合,我称之为ViewModel

  1. @Composable
  2. fun PokemonList(
  3. navController: NavController,
  4. viewModel: PokemonListViewModel = hiltViewModel()
  5. ) {
  6. val pokemonList by remember { viewModel.pokemonList }
  7. val endReached by remember { viewModel.endReached }
  8. val loadError by remember { viewModel.loadError }
  9. val isLoading by remember { viewModel.isLoading }
  10. LazyColumn(contentPadding = PaddingValues(16.dp)) {
  11. val itemCount = if(pokemonList.size % 2 == 0) pokemonList.size / 2 else pokemonList.size / 2 + 1
  12. items(itemCount) {
  13. if (it >= itemCount - 1 && !endReached){
  14. viewModel.loadPokemonPaginated()
  15. }
  16. PokedexRow(rowIndex = it, entries = pokemonList, navController = navController)
  17. }
  18. }
  19. }

ViewModel(仅第一个代码)

  1. @HiltViewModel
  2. class PokemonListViewModel @Inject constructor(
  3. private val repository: PokemonRepository
  4. ) : ViewModel() {
  5. private var curPage = 0
  6. var pokemonList = mutableStateOf<List<PokedexListEntry>>(listOf())
  7. var loadError = mutableStateOf("")
  8. var isLoading = mutableStateOf(false)
  9. var endReached = mutableStateOf(false)
  10. init {
  11. loadPokemonPaginated()
  12. }

**编写版本:**1.1.1
Kotlin版本1.6.10
不确定性

  1. implementation 'androidx.core:core-ktx:1.7.0'
  2. implementation "androidx.compose.ui:ui:$compose_version"
  3. implementation "androidx.compose.material:material:$compose_version"
  4. implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
  5. implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
  6. implementation 'androidx.activity:activity-compose:1.4.0'
  7. testImplementation 'junit:junit:4.13.2'
  8. androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  9. androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
  10. androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
  11. debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
  12. implementation("com.google.dagger:hilt-android:2.41")
  13. kapt("com.google.dagger:hilt-android-compiler:2.38.1")
  14. // Retrofit
  15. implementation 'com.squareup.retrofit2:retrofit:2.9.0'
  16. implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
  17. implementation "com.squareup.okhttp3:okhttp:4.9.3"
  18. implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"
  19. // Timber
  20. implementation 'com.jakewharton.timber:timber:4.7.1'
  21. // Coroutines
  22. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
  23. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'
  24. // Coroutine Lifecycle Scopes
  25. implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
  26. implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"
  27. // Coil
  28. implementation "io.coil-kt:coil:1.3.2"
  29. implementation "com.google.accompanist:accompanist-coil:0.7.0"
  30. //Dagger - Hilt
  31. implementation 'com.google.dagger:hilt-android:2.41'
  32. kapt 'com.google.dagger:hilt-compiler:2.41'
  33. kapt "androidx.hilt:hilt-compiler:1.0.0"
  34. implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'
  35. //Navigation
  36. implementation "androidx.navigation:navigation-compose:2.4.2"
  37. //Palette
  38. implementation "androidx.palette:palette:1.0.0"

9o685dep

9o685dep1#

z8dt9xmd

z8dt9xmd2#

如果这能有所帮助,我也有同样的问题。我设法解决它,通过添加到我的build.gradle在我的应用程序模块

  1. id("kotlin-kapt")
  2. id("dagger.hilt.android.plugin")

字符串
这在我的根构建.gradle在插件部分

  1. id("com.google.dagger.hilt.android") version "2.44" apply false


以及我的应用build.gradle中的所有这些依赖项

  1. implementation("com.google.dagger:hilt-android:2.44")
  2. kapt("com.google.dagger:hilt-android-compiler:2.44")
  3. implementation("androidx.hilt:hilt-work:1.0.0")
  4. kapt("androidx.hilt:hilt-compiler:1.0.0")
  5. implementation("androidx.work:work-runtime-ktx:2.8.1")
  6. implementation("androidx.hilt:hilt-navigation-compose:1.0.0")


这是我第一次在使用Hilt时遇到这个问题。我想这是因为我没有像以前那样在root build.gradle和app build.gradle中完全安装Hilt。

展开查看全部

相关问题