Skip to content
The Chat Archive
gpt-4-0314· OpenAI WildChat-4.8M (AI2)

I/ezermusicplaye: Background young concurrent copying GC freed 21464(2540KB) AllocSpace…

  1. User

    I/ezermusicplaye: Background young concurrent copying GC freed 21464(2540KB) AllocSpace objects, 0(0B) LOS objects, 84% free, 4594KB/28MB, paused 1.608ms,565us total 113.024ms W/System: A resource failed to call close. E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.deezermusicplayer, PID: 25360 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 androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:104) at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:67) at com.example.deezermusicplayer.MainActivity.NavigationHost(MainActivity.kt:87) at com.example.deezermusicplayer.MainActivityHomeScreenHomeScreen2.invoke(MainActivity.kt:138) at com.example.deezermusicplayer.MainActivityHomeScreenHomeScreen2.invoke(MainActivity.kt:137) 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.ScaffoldKtScaffoldLayoutScaffoldLayout1111bodyContentPlaceablesbodyContentPlaceables1.invoke(Scaffold.kt:322) at androidx.compose.material.ScaffoldKtScaffoldLayoutScaffoldLayout1111bodyContentPlaceablesbodyContentPlaceables1.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.LayoutNodeSubcompositionsStatesubcomposesubcompose3111.invoke(SubcomposeLayout.kt:778) at androidx.compose.ui.layout.LayoutNodeSubcompositionsStatesubcomposesubcompose3111.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.ComposerImpldoComposedoCompose25.invoke(Composer.kt:3373)atandroidx.compose.runtime.ComposerImpl5.invoke(Composer.kt:3373) at androidx.compose.runtime.ComposerImpldoCompose225.invoke(Composer.kt:3363) at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341) at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1) at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3363) at androidx.compose.runtime.ComposerImpl.composeContentruntimerelease(Composer.kt:3298)atandroidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:587)atandroidx.compose.runtime.Recomposer.composeInitialruntime_release(Composer.kt:3298) at androidx.compose.runtime.CompositionImpl.composeContent(Composition.kt:587) at androidx.compose.runtime.Recomposer.composeInitialruntime_release(Recomposer.kt:966) at androidx.compose.runtime.ComposerImplCompositionContextImpl.composeInitialCompositionContextImpl.composeInitialruntime_release(Composer.kt:3973) at androidx.compose.runtime.ComposerImplCompositionContextImpl.composeInitialCompositionContextImpl.composeInitialruntime_release(Composer.kt:3973) at androidx.compose.runtime.CompositionImpl.setContent(Composition.kt:519) at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcomposeInto(SubcomposeLayout.kt:466) at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:439) at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:430) at androidx.compose.ui.layout.LayoutNodeSubcompositionsState.subcompose(SubcomposeLayout.kt:419) at androidx.compose.ui.layout.LayoutNodeSubcompositionsStateScope.subcompose(SubcomposeLayout.kt:740)atandroidx.compose.material.ScaffoldKtScope.subcompose(SubcomposeLayout.kt:740) at androidx.compose.material.ScaffoldKtScaffoldLayout1111.invoke(Scaffold.kt:320)atandroidx.compose.material.ScaffoldKt1.invoke(Scaffold.kt:320) at androidx.compose.material.ScaffoldKtScaffoldLayout1111.invoke(Scaffold.kt:243)atandroidx.compose.ui.layout.MeasureScope1.invoke(Scaffold.kt:243) at androidx.compose.ui.layout.MeasureScopelayout1.placeChildren(MeasureScope.kt:70)atandroidx.compose.ui.layout.LayoutNodeSubcompositionsState1.placeChildren(MeasureScope.kt:70) at androidx.compose.ui.layout.LayoutNodeSubcompositionsStatecreateMeasurePolicy11measure1.placeChildren(SubcomposeLayout.kt:610)atandroidx.compose.ui.node.LayoutNodeLayoutDelegate1.placeChildren(SubcomposeLayout.kt:610) at androidx.compose.ui.node.LayoutNodeLayoutDelegateMeasurePassDelegatelayoutChildrenlayoutChildren11.invoke(LayoutNodeLayoutDelegate.kt:276)atandroidx.compose.ui.node.LayoutNodeLayoutDelegate1.invoke(LayoutNodeLayoutDelegate.kt:276) at androidx.compose.ui.node.LayoutNodeLayoutDelegateMeasurePassDelegatelayoutChildrenlayoutChildren11.invoke(LayoutNodeLayoutDelegate.kt:268)E/AndroidRuntime:atandroidx.compose.runtime.snapshots.Snapshot1.invoke(LayoutNodeLayoutDelegate.kt:268) E/AndroidRuntime: at androidx.compose.runtime.snapshots.SnapshotCompanion.observe(Snapshot.kt:2200) -> this is the error i got and this is the MainActivity->package com.example.deezermusicplayer

    import CategoriesViewModel import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.padding import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.navigation.NavController import androidx.navigation.NavHostController import androidx.navigation.compose.NavHost import androidx.navigation.compose.composable import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController import com.example.deezermusicplayer.ui.theme.DeezerMusicPlayerTheme

    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
        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 explain and fix the issue for me

  2. Assistant

    The cause of the problem is that NavController.setViewModelStore() must be called for a NavHostController before the NavHost is created. I can help you fix this issue by setting the ViewModelStore for your NavController.

    First, add the missing import statement for CategoriesViewModel at the top of your MainActivity.kt file:

    import com.example.deezermusicplayer.CategoriesViewModel
    

    Then modify the NavigationHost function to set the ViewModelStore of the NavHostController. Here's the updated code:

    @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, 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)
            }
        }
    }
    

    Now your app should run without the IllegalStateException. If you encounter any other issues or need further assistance, feel free to ask.