D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.deezermusicplayer, PID: 31759
java.lang.IllegalStateException: ViewModelStore should be set before setGraph call
at androidx.navigation.NavController.setViewModelStore(NavController.kt:2169)
at androidx.navigation.NavHostController.setViewModelStore(NavHostController.kt:101)
at com.example.deezermusicplayer.MainActivity.NavigationHost(MainActivity.kt:95)
at com.example.deezermusicplayer.MainActivityHomeScreen2.invoke(MainActivity.kt:149)
at com.example.deezermusicplayer.MainActivityHomeScreen2.invoke(MainActivity.kt:148)
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.material.ScaffoldKt111.invoke(Scaffold.kt:322)
at androidx.compose.material.ScaffoldKt111.invoke(Scaffold.kt:320)
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.ui.layout.LayoutNodeSubcompositionsState31.invoke(SubcomposeLayout.kt:778)
at androidx.compose.ui.layout.LayoutNodeSubcompositionsState31.invoke(SubcomposeLayout.kt:446)
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.ActualJvm_jvmKt.invokeComposable(ActualJvm.jvm.kt:78)
at androidx.compose.runtime.ComposerImpl2doCompose5.invoke(Composer.kt:3363) -> i had this error , this is my MainActivity.kt ->package com.example.deezermusicplayer
Assistant
The error you are facing, "ViewModelStore should be set before setGraph call", occurs because you need to set the ViewModelStore before you set up the graph in your NavHost. So, let's move the logic of setting ViewModelStore before creating the NavHost.
class MainActivity : ComponentActivity() {
private val bottomNavigationItems = listOf(
Screen.Home,
Screen.Favorites
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
DeezerMusicPlayerTheme {
// Set up navigation
val navController = rememberNavController()
val bottomNavigationItems = listOf(
Screen.Home,
Screen.Favorites
)
Scaffold(
bottomBar = {
BottomNavigation {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
bottomNavigationItems.forEach { screen ->
BottomNavigationItem(
icon = {
Icon(
imageVector = screen.icon,
contentDescription = null
)
},
selected = currentRoute == screen.route,
onClick = {
navController.navigate(screen.route) {
// Pop up to the start destination of the current graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.startDestinationId) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
restoreState = true
}
},
label = {
Text(text = screen.title)
}
)
}
}
},
content = { paddingValues ->
NavigationHost(
navController = navController,
modifier = Modifier.padding(paddingValues)
)
}
)
}
}
}
@Composable
fun NavigationHost(
navController: NavController,
modifier: Modifier = Modifier,
) {
val categoriesViewModel = remember { CategoriesViewModel() }
val categories by categoriesViewModel.categories.collectAsState() // use collectAsState here to collect the latest state
// Set the ViewModelStore before creating the NavHost:
if (navController is NavHostController) {
navController.setViewModelStore(ViewModelStore())
}
NavHost(navController = navController as NavHostController, startDestination = Screen.Home.route) {
composable(Screen.Home.route) {
HomeScreen(categories = categories, navController = navController, modifier = modifier)
}
composable(Screen.Favorites.route) {
FavoritesScreen(categories = categories, navController = navController, modifier = modifier)
}
}
}
@Composable
fun HomeScreen(
categories: List<Category>,
navController: NavController,
modifier: Modifier = Modifier
) {
Scaffold(bottomBar = {
BottomNavigation {
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
bottomNavigationItems.forEach { screen ->
BottomNavigationItem(
icon = {
Icon(
imageVector = screen.icon,
contentDescription = null
)
},
selected = currentRoute == screen.route,
onClick = {
navController.navigate(screen.route) {
// Pop up to the start destination of the current graph to
// avoid building up a large stack of destinations
// on the back stack as users select items
popUpTo(navController.graph.startDestinationId) {
saveState = true
}
// Avoid multiple copies of the same destination when
// reselecting the same item
launchSingleTop = true
restoreState = true
}
},
label = {
Text(text = screen.title)
}
)
}
}
}, content = { paddingValues ->
NavigationHost(
navController = navController,
modifier = Modifier.padding(paddingValues)
)
}
)
}
@Composable
fun FavoritesScreen(categories: List<Category>, navController: NavController, modifier: Modifier = Modifier) {
}
}
can you fix the issue and explain to me
@Composable
fun NavigationHost(
navController: NavController,
modifier: Modifier = Modifier,
) {
val categoriesViewModel = remember { CategoriesViewModel() }
val categories by categoriesViewModel.categories.collectAsState() // use collectAsState here to collect the latest state
// Set the ViewModelStore before creating the NavHost:
if (navController is NavHostController) {
navController.setViewModelStore(ViewModelStore())
}
NavHost(navController = navController as NavHostController, startDestination = Screen.Home.route) {
composable(Screen.Home.route) {
HomeScreen(categories = categories, navController = navController, modifier = modifier)
}
composable(Screen.Favorites.route) {
FavoritesScreen(categories = categories, navController = navController, modifier = modifier)
}
}
}
Here, we added a condition to check if navController is an instance of NavHostController, and if it is, we set its ViewModelStore before the NavHost is created. This should fix the error you're having.
Also, it looks like you have duplicate code for BottomNavigation in both MainActivity and HomeScreen. You should remove it from the HomeScreen because the Scaffold in MainActivity is already providing the BottomNavigation. Removing the duplicate BottomNavigation from HomeScreen will make it look like this: