简体   繁体   English

使用 Jetpack Compose 导航时无法创建视图模型的实例

[英]Cannot create an instance of viewmodel while using Jetpack Compose navigation

I am using navigation component for jetpack compose in my app like this:我在我的应用程序中使用 Jetpack Compose 的导航组件,如下所示:

@Composable
fun FoodiumNavigation() {
    val navController = rememberNavController()

    NavHost(
        navController = navController,
        startDestination = Screen.Main.route,
    ) {
        composable(Screen.Main.route) {
            MainScreen(navController)
        }

        ...
    }
}

And I am getting viewmodel in my MainScreen composable like this:我在我的MainScreen可组合项中获取viewmodel ,如下所示:

@Composable
fun MainScreen(navController: NavController) {
    val mainViewModel: MainViewModel = viewModel()
    ...
}

which is giving me a runtime exception as Cannot create an instance of class com.package.main.MainViewModel .这给了我一个运行时异常,因为Cannot create an instance of class com.package.main.MainViewModel

Here, I am stating that this only happens while using navigation component, ie everything was working fine and mainViewModel was successfully instantiated before using navigation component in my app.在这里,我声明这只会在使用导航组件时发生,即一切正常,并且mainViewModel在我的应用程序中使用导航组件之前已成功实例化。

The MainViewModel is like this: MainViewModel是这样的:

@ExperimentalCoroutinesApi
@HiltViewModel
class MainViewModel @Inject constructor(private val postRepository: PostRepository) :
    ViewModel() {

    private val _postsLiveDataState = MutableLiveData<UiState<List<Post>>>()
    val postLiveState: LiveData<UiState<List<Post>>> = _postsLiveDataState

    init {
        getPostsState()
    }

    private fun getPostsState() {
        viewModelScope.launch {
            postRepository.getAllPosts()
                .onStart { _postsLiveDataState.value = UiState(loading = true) }
                .map { resource -> UiState.fromResource(resource) }
                .collect { state -> _postsLiveDataState.value = state }
        }
    }
}

If your @HiltViewModel is scoped to the navigation graph use hiltNavGraphViewModel() instead of viewModel() to initialize.如果您的 @HiltViewModel 范围为导航图,请使用 hiltNavGraphViewModel() 而不是 viewModel() 进行初始化。 For more reference android documentaion更多参考android 文档

hiltNavGraphViewModel is deprecated, should be used hiltViewModel() instead不推荐使用hiltNavGraphViewModel ,应改为使用hiltViewModel()

also add dependency androidx.hilt:hilt-navigation-compose:1.0.0-alpha03还添加依赖androidx.hilt:hilt-navigation-compose:1.0.0-alpha03

You should add this你应该添加这个

implementation("androidx.hilt:hilt-navigation-compose:1.0.0")

then you can use this code for create instance of your viewmodel然后您可以使用此代码创建视图模型的实例

val viewModel: YourViewModelClass= hiltViewModel()

You can use viewModel() as well, but check that owning Activity or Fragment has been annotated with @AndroidEntryPoint .您也可以使用viewModel() ,但请检查拥有的ActivityFragment是否已使用@AndroidEntryPoint进行注释。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM