android 给定的组件没有实现接口dagger.hilt.internal.GeneratedComponent?

wwtsj6pe  于 2023-05-15  发布在  Android
关注(0)|答案(1)|浏览(155)

我正在开发Android应用程序使用喷气背包撰写和匕首刀柄,但当我运行项目,我得到以下异常

java.lang.IllegalStateException: Given component holder class com.example.readerapp.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
                                                                                                        at dagger.hilt.EntryPoints.get(EntryPoints.java:62)
                                                                                                        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.createInternal(HiltViewModelFactory.java:149)
                                                                                                        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.createInternal(HiltViewModelFactory.java:143)
                                                                                                        at androidx.hilt.navigation.HiltViewModelFactory.create(HiltNavBackStackEntry.kt:56)
                                                                                                        at androidx.hilt.navigation.compose.HiltViewModelKt.createHiltViewModelFactory(HiltViewModel.kt:56)
                                                                                                        at com.example.readerapp.navigation.ReaderNavigationKt$ReaderNavigation$1$5.invoke(ReaderNavigation.kt:47)
                                                                                                        at com.example.readerapp.navigation.ReaderNavigationKt$ReaderNavigation$1$5.invoke(ReaderNavigation.kt:37)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$4$1.invoke(NavHost.kt:150)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$4$1.invoke(NavHost.kt:149)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
                                                                                                        at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:60)
                                                                                                        at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1)
                                                                                                        at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:52)
                                                                                                        at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider$1.invoke(NavBackStackEntryProvider.kt:51)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:228)
                                                                                                        at androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider(NavBackStackEntryProvider.kt:47)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$4.invoke(NavHost.kt:149)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$4.invoke(NavHost.kt:142)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.animation.CrossfadeKt$Crossfade$5$1.invoke(Crossfade.kt:133)
                                                                                                        at androidx.compose.animation.CrossfadeKt$Crossfade$5$1.invoke(Crossfade.kt:128)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
                                                                                                        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
                                                                                                        at androidx.compose.animation.CrossfadeKt.Crossfade(Crossfade.kt:142)
                                                                                                        at androidx.compose.animation.CrossfadeKt.Crossfade(Crossfade.kt:73)
                                                                                                        at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:142)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$5.invoke(Unknown Source:13)
                                                                                                        at androidx.navigation.compose.NavHostKt$NavHost$5.invoke(Unknown Source:10)
                                                                                                        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:162)
                                                                                                        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2465)
                                                                                                        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2733)
                                                                                                        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3364)

在我的ReaderNavigation.kt下面

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun ReaderNavigation() {
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = ReaderScreens.SplashScreen.name) {
        composable(ReaderScreens.SplashScreen.name) {

            ReaderSplashScreen(navController = navController)
        }
        composable(ReaderScreens.LoginScreen.name) {

            ReaderLoginScreen(navController = navController)
        }
        composable(ReaderScreens.ReaderStatsScreen.name) {
            ReaderStatsScreen(navController = navController)
        }
        composable(ReaderScreens.ReaderHomeScreen.name) {
            Home(navController = navController)
        }

        composable(ReaderScreens.SearchScreen.name) {
            val searchViewModel = hiltViewModel<BookSearchViewModel>()
            SearchScreen(navController = navController, viewModel = searchViewModel)
        }
    }
}

在我的MainActivity.kt下面

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.readerapp.navigation.ReaderNavigation
import com.example.readerapp.ui.theme.ReaderAppTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ReaderAppTheme {
                ReaderApp()

                // A surface container using the 'background' color from the theme

            }
        }
    }
}

@Composable
fun ReaderApp() {
    Surface(
        modifier = Modifier
            .fillMaxSize(),
        //.padding(top = 46.dp),
        color = MaterialTheme.colors.background,
    ) {
        Column(
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            ReaderNavigation()
        }

    }
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    ReaderAppTheme {
        //  ReaderApp()
    }
}

在我的BookSearchViewModel.kt下面

@HiltViewModel
class BookSearchViewModel @Inject constructor(private val repository: BookRepository) : ViewModel() {
    var list: List<Item> by mutableStateOf(listOf())
    var isLoading: Boolean by mutableStateOf(true)
    init {
        loadBooks()
    }

    private fun loadBooks() {
        searchBooks("flutter")
    }

    fun searchBooks(query: String) {
        viewModelScope.launch {

            if (query.isEmpty()){
                return@launch
            }
            try {
                when(val response = repository.getBooks(query)) {
                    is Resource.Success -> {
                        list = response.data!!
                        if (list.isNotEmpty()) isLoading = false
                    }
                    is Resource.Error -> {
                        isLoading = false
                        Log.e("Network", "searchBooks: Failed getting books", )
                    }
                    else -> {isLoading = false}
                }

            }catch (exception: Exception){
                isLoading = false
                Log.d("Network", "searchBooks: ${exception.message.toString()}")
            }

        }

    }

}

我使用匕首刀柄2.46版本我已经尝试了以下stackoverflow答案Given component holder class packagename.MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent和其他所有答案它没有帮助我已经添加AndroidEntryPoint到MainActivity以及所有必要的刀柄依赖以及我想知道我到底在哪里犯了错误

bkhjykvo

bkhjykvo1#

我修复了我的问题,更新jetpack撰写导航版本从1.0.0到1.1.0-alpha 01

implementation("androidx.hilt:hilt-navigation-compose:1.1.0-alpha01")

在app.gradle文件中

相关问题