https://kotlinlang.org logo
#compose
Title
# compose
a

Android75

04/09/2022, 10:01 AM
I am using Hilt and navigation so when inject viewmodel without parameters it works but when viewModel has other classes injected i has crash
Caused by: java.lang.InstantiationException: java.lang.Class<com.xxx.screen.section.SViewModel> has no zero argument constructor
n

Nat Strangerweather

04/09/2022, 10:02 AM
Have you tried this:
Copy code
val viewModel: MainViewModel = hiltViewModel()
That's what worked for me.
a

Android75

04/09/2022, 10:02 AM
ok i try thanks
so this
Copy code
val viewModel = viewModel<SViewModel>()
    SectionPage(viewModel, backStackEntry.arguments?.getString("type"))
can’t work
n

Nat Strangerweather

04/09/2022, 10:04 AM
No, no, I mean when you are calling your viewModel in your Composable
a

Android75

04/09/2022, 10:04 AM
i create viewModel in navifation
n

Nat Strangerweather

04/09/2022, 10:04 AM
Ah ok, sorry
a

Android75

04/09/2022, 10:05 AM
Copy code
composable(Destination.SectionScreenPage.path) { backStackEntry ->
    val viewModel = viewModel<SViewModel>()
        SectionPage(viewModel)
    
    
    }

}
n

Nat Strangerweather

04/09/2022, 10:06 AM
Have you tried replacing
val viewModel = viewModel<SViewModel>()
with
val viewModel: SViewModel = hiltViewModel()
?
a

Android75

04/09/2022, 10:07 AM
it works, great
n

Nat Strangerweather

04/09/2022, 10:07 AM
cool
3 Views