Несоответствие типа: NavController (NavHost)
Я новичок в Jetpack Compose Kotlin. Несоответствие типов: предполагаемый тип — NavController, но ожидался NavHostController. Фрагмент кода в MainActivity, который вызывает ошибку NavHost(navController = navController, startDestination = APPS_ROUTE) {
fun BodyContent(paddingValues: PaddingValues, navController: NavController) {
Column(modifier = Modifier.padding(paddingValues)) {
NavHost(navController = navController, startDestination = APPS_ROUTE) {
composable(APPS_ROUTE) { AppsScreen() }
composable(WIDGETS_ROUTE) { WidgetsScreen() }
composable(SETTINGS_ROUTE) { SettingsScreen() }
}
}
}
Несоответствие типов Обязательно: NavHostController Найдено: параметр-значение NavController navController: NavController
Это мой код в MainActivity.kt
package hr.filipal.iconflex
import android.os.Bundle
import androidx.navigation.NavController
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.*
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Widgets
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import hr.filipal.iconflex.AppDestinations.APPS_ROUTE
import hr.filipal.iconflex.AppDestinations.SETTINGS_ROUTE
import hr.filipal.iconflex.AppDestinations.WIDGETS_ROUTE
import hr.filipal.iconflex.screens.AppsScreen
import hr.filipal.iconflex.screens.SettingsScreen
import hr.filipal.iconflex.screens.WidgetsScreen
import hr.filipal.iconflex.ui.theme.IconFlexTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
IconFlexTheme {
MainScreen()
}
}
}
}
@Composable
fun MainScreen() {
val navController = rememberNavController()
Scaffold(
topBar = { SearchBar() },
bottomBar = { BottomNavigationBar(navController) }
) { innerPadding ->
BodyContent(innerPadding, navController)
}
}
@Composable
fun SearchBar() {
// Implementacija trake za pretragu
TopAppBar(
title = {
TextField(
value = "",
onValueChange = { /* Ovdje ažurirate state s novom vrijednošću */ },
placeholder = { Text("Search apps") },
modifier = Modifier.fillMaxSize()
)
},
backgroundColor = MaterialTheme.colors.primarySurface
)
}
@Composable
fun BottomNavigationBar(navController: NavController) {
val currentRoute = navController.currentBackStackEntryAsState().value?.destination?.route
BottomNavigation {
BottomNavigationItem(
icon = { Icon(Icons.Filled.Home, contentDescription = null) },
label = { Text(stringResource(R.string.navigation_apps)) },
selected = currentRoute == APPS_ROUTE,
onClick = {
if (currentRoute != APPS_ROUTE) {
navController.navigate(APPS_ROUTE)
}
}
)
BottomNavigationItem(
icon = { Icon(Icons.Filled.Widgets, contentDescription = null) },
label = { Text(stringResource(R.string.navigation_widgets)) },
selected = currentRoute == WIDGETS_ROUTE,
onClick = {
if (currentRoute != WIDGETS_ROUTE) {
navController.navigate(WIDGETS_ROUTE)
}
}
)
BottomNavigationItem(
icon = { Icon(Icons.Filled.Settings, contentDescription = null) },
label = { Text(stringResource(R.string.navigation_settings)) },
selected = currentRoute == SETTINGS_ROUTE,
onClick = {
if (currentRoute != SETTINGS_ROUTE) {
navController.navigate(SETTINGS_ROUTE)
}
}
)
}
}
@Composable
fun BodyContent(paddingValues: PaddingValues, navController: NavController) {
Column(modifier = Modifier.padding(paddingValues)) {
NavHost(navController = navController, startDestination = APPS_ROUTE) {
composable(APPS_ROUTE) { AppsScreen() }
composable(WIDGETS_ROUTE) { WidgetsScreen() }
composable(SETTINGS_ROUTE) { SettingsScreen() }
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
IconFlexTheme {
MainScreen()
}
}
Это мой Build.gradle.kts
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "hr.filipal.iconflex"
compileSdk = 34
defaultConfig {
applicationId = "hr.filipal.iconflex"
minSdk = 28
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.4"
}
packaging {
resources.excludes.add("/META-INF/{AL2.0,LGPL2.1}")
}
buildToolsVersion = "34.0.0"
}
dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.2")
implementation("androidx.activity:activity-compose:1.8.0")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3:1.1.2")
implementation("androidx.compose.material3:material3-window-size-class:1.1.2")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.9.20")
implementation("androidx.recyclerview:recyclerview:1.3.2")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.google.android.material:material:1.10.0")
implementation("androidx.navigation:navigation-fragment-ktx:2.7.5")
implementation("androidx.navigation:navigation-ui-ktx:2.7.5")
implementation("androidx.compose.material3:material3:1.1.2")
implementation("androidx.compose.material:material:1.5.4")
implementation("androidx.navigation:navigation-compose:2.7.5")
implementation("androidx.compose.material:material-icons-extended:1.5.4")
implementation("androidx.compose.foundation:foundation:1.5.4")
implementation("androidx.compose.ui:ui:1.5.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
Как я могу решить эту проблему? Спасибо
Я пытался добавить import androidx.navigation.compose.NavHostController и другие части пакета, но это не помогло.