Search code examples
androidkotlinandroid-jetpack-composeandroid-jetpack-navigation

How do I fix "Unresolved reference: composable" and "Interface NavHost does not have constructors" issues in my Composable Kotlin code?


I'm new to Kotlin and Android studio/Compose, so I;m not exactly sure how to fix this. I tried following the suggestions from Android Studio and looked around online, but no luck. I'm trying to make a NavHost for navigation, and the NavHost and all of the composable(route = ) are throwing an error. Here's my code:

package com.example.recipesavercompose.navigation

import androidx.compose.runtime.Composable
import androidx.navigation.NavDestination
import androidx.navigation.NavHost
import androidx.navigation.NavHostController
import androidx.navigation.compose.composable
import com.example.recipesavercompose.compose.Culinary_Terms
import com.example.recipesavercompose.compose.Drafts
import com.example.recipesavercompose.compose.Ingredient_Substitutions
import com.example.recipesavercompose.compose.Recipes
import com.example.recipesavercompose.compose.Settings
import com.example.recipesavercompose.compose.View_Recipe


@Composable
fun NavGraph (navController: NavHostController){
    val initialDestination = NavDestination.createRoute(Screens.Recipes.route)
    NavHost(navController = navController, startDestination = initialDestination)
    {
        composable(route = Screens.Recipes.route){
            Recipes()
        }
        composable(route = Screens.CulinaryTerms.route){
            Culinary_Terms()
        }
        composable(route = Screens.Drafts.route){
            Drafts()
        }
        composable(route = Screens.IngredientSubstitutions.route){
            Ingredient_Substitutions()
        }
        composable(route = Screens.Settings.route){
            Settings()
        }
        composable(route = Screens.ViewRecipe.route){
            View_Recipe()
        }
    }
}

I tried looking at my imports, but they seemed fine.


Solution

  • Import the correct compose navigation dependencies:

    import androidx.navigation.compose.NavHost
    import androidx.navigation.compose.rememberNavController
    
    ...
    
    val navController = rememberNavController()
    NavHost(
        navController = navController,
        startDestination = "start_screen"
    ) {
        composable("start_screen") {
             // Your Composable
        }
    }