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

hpxqektj  于 12个月前  发布在  Kotlin
关注(0)|答案(2)|浏览(229)

我正在学习使用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应用程序:

@HiltAndroidApp
class PokedexApplication : Application() {
  override fun onCreate() {
    super.onCreate()
    Timber.plant(Timber.DebugTree())
  }
}

字符串

主活动

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
      PokedexTheme {
        val navController = rememberNavController()
        NavHost(navController = navController,
          startDestination = "pokemon_list_screen"
        ) {
          composable("pokemon_list_screen") {
            PokemonListScreen(navController = navController)
          }
          composable(
            "pokemon_detail_screen/{dominantColor}/{pokemonName}",
            arguments = listOf(
              navArgument("dominantColor") {
                type = NavType.IntType
              },
              navArgument("pokemonName") {
                type = NavType.StringType
              }
            )) {
            val dominantColor = remember {
              val color = it.arguments?.getInt("dominantColor")
              color?.let { Color(it) } ?: Color.White
            }
            val pokemonName = remember {
              it.arguments?.getString("pokemonName")

            }
          }
        }
      }
    }
  }
}

AppModule

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

  @Singleton
  @Provides
  fun providePokemonRepository(
    api: PokeApi
  ) = PokemonRepository(api)

  @Singleton
  @Provides
  fun providePokeApi(): PokeApi {
    return Retrofit.Builder()
      .addConverterFactory(GsonConverterFactory.create())
      .baseUrl(BASE_URL)
      .build()
      .create(PokeApi::class.java)
  }
}

仓库

@ActivityScoped
class PokemonRepository @Inject constructor (
  private val api: PokeApi
  ) {

  suspend fun getPokemonList(limit: Int, offset: Int): Resource<PokemonList> {
    val response = try {
      api.getPokemonList(limit, offset)
    } catch (e: Exception) {
      return Resource.Error(e.message.toString())
    }
    return Resource.Success(response)
  }
}

可组合,我称之为ViewModel

@Composable
fun PokemonList(
  navController: NavController,
  viewModel: PokemonListViewModel = hiltViewModel()
) {

  val pokemonList by remember { viewModel.pokemonList }
  val endReached by remember { viewModel.endReached }
  val loadError by remember { viewModel.loadError }
  val isLoading by remember { viewModel.isLoading }

  LazyColumn(contentPadding = PaddingValues(16.dp)) {
    val itemCount = if(pokemonList.size % 2 == 0) pokemonList.size / 2 else pokemonList.size / 2 + 1

    items(itemCount) {
      if (it >= itemCount - 1 && !endReached){
        viewModel.loadPokemonPaginated()
      }
      PokedexRow(rowIndex = it, entries = pokemonList, navController = navController)
    }
  }
}

ViewModel(仅第一个代码)

@HiltViewModel
class PokemonListViewModel @Inject constructor(
  private val repository: PokemonRepository
) : ViewModel() {

  private var curPage = 0

  var pokemonList = mutableStateOf<List<PokedexListEntry>>(listOf())
  var loadError = mutableStateOf("")
  var isLoading = mutableStateOf(false)
  var endReached = mutableStateOf(false)

  init {
    loadPokemonPaginated()
  }

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

implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.1'
implementation 'androidx.activity:activity-compose:1.4.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"

implementation("com.google.dagger:hilt-android:2.41")
kapt("com.google.dagger:hilt-android-compiler:2.38.1")

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation "com.squareup.okhttp3:okhttp:4.9.3"
implementation "com.squareup.okhttp3:logging-interceptor:4.9.0"

// Timber
implementation 'com.jakewharton.timber:timber:4.7.1'

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0'

// Coroutine Lifecycle Scopes
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.1"

// Coil
implementation "io.coil-kt:coil:1.3.2"
implementation "com.google.accompanist:accompanist-coil:0.7.0"

//Dagger - Hilt
implementation 'com.google.dagger:hilt-android:2.41'
kapt 'com.google.dagger:hilt-compiler:2.41'
kapt "androidx.hilt:hilt-compiler:1.0.0"
implementation 'androidx.hilt:hilt-navigation-compose:1.0.0'

//Navigation
implementation "androidx.navigation:navigation-compose:2.4.2"

//Palette
implementation "androidx.palette:palette:1.0.0"

9o685dep

9o685dep1#

z8dt9xmd

z8dt9xmd2#

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

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

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

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


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

implementation("com.google.dagger:hilt-android:2.44")
kapt("com.google.dagger:hilt-android-compiler:2.44")
implementation("androidx.hilt:hilt-work:1.0.0")
kapt("androidx.hilt:hilt-compiler:1.0.0")
implementation("androidx.work:work-runtime-ktx:2.8.1")
implementation("androidx.hilt:hilt-navigation-compose:1.0.0")


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

相关问题