gpt4 book ai didi

android - 如何将 `rememberNavController` 从喷气背包组合成使用刀柄的 Activity ?

转载 作者:行者123 更新时间:2023-12-04 23:57:28 27 4
gpt4 key购买 nike

我正在尝试注入(inject)我的 navHostController进入我的MainActivity使用刀柄。但是在尝试编译代码时出现以下错误:

> Task :app:kaptDebugKotlin
C:\Users\pierr\AndroidStudioProjects\AndroidApps\Compose\Udemy\course01\crud\app\build\generated\source\kapt\debug\com\example\crud\CrudApplication_HiltComponents.java:129: error: [Dagger/MissingBinding] androidx.navigation.NavHostController cannot be provided without an @Inject constructor or an @Provides-annotated method.
public abstract static class SingletonC implements CrudApplication_GeneratedInjector,
^
androidx.navigation.NavHostController is injected at
com.example.crud.ui.MainActivity.navHostController
com.example.crud.ui.MainActivity is injected at
com.example.crud.ui.MainActivity_GeneratedInjector.injectMainActivity(com.example.crud.ui.MainActivity) [com.example.crud.CrudApplication_HiltComponents.SingletonC ? com.example.crud.CrudApplication_HiltComponents.ActivityRetainedC ? com.example.crud.CrudApplication_HiltComponents.ActivityC]

> Task :app:kaptDebugKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 5s
24 actionable tasks: 2 executed, 22 up-to-date

这是我的 MainActivity代码:
package com.example.crud.ui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.navigation.NavHostController
import com.example.crud.navigation.NavigationComponent
import com.example.crud.ui.theme.CRUDTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : ComponentActivity() {

@Inject lateinit var navHostController: NavHostController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContent {
CRUDTheme {
Surface(color = MaterialTheme.colors.background) {
NavigationComponent(navHostController)
}
}
}
}
}
这是我的 NavigationModule代码:
package com.example.crud.di

import androidx.compose.runtime.Composable
import androidx.navigation.compose.rememberNavController
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent

@Module
@InstallIn(ActivityComponent::class)
object NavigationModule {
@Composable
fun provideNavHostController() = rememberNavController()
}
这是 NavigationComponent 的代码(它将 navHostController 我试图注入(inject) MainActivity 作为参数):
package com.example.crud.navigation

import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import com.example.crud.ui.screens.crud.details.DetailScreen
import com.example.crud.ui.screens.crud.register.RegisterScreen
import com.example.crud.ui.screens.home.HomeScreen
import com.example.crud.ui.screens.home.HomeViewModel

@Composable
fun NavigationComponent(navController: NavHostController) {
NavHost(navController = navController, startDestination = Routes.HOME) {
composable(Routes.HOME) {
val homeViewModel: HomeViewModel = hiltViewModel()
val cities = homeViewModel.cities.observeAsState(listOf())
HomeScreen(
cities = cities,
navigateToDetailsAction = { navController.navigate(Routes.REGISTER) }
) { cityId ->
navController.navigate(Routes.getDetailsDynamicRoute(cityId))
}
}
composable(Routes.REGISTER) { RegisterScreen { navController.popBackStack() } }
composable(
route = Routes.DETAILS,
arguments = listOf(navArgument(Routes.CITY_ID_KEY) { type = NavType.IntType })
) { backStackEntry ->
val cityId = backStackEntry.arguments?.getInt(Routes.CITY_ID_KEY)
cityId?.let {
DetailScreen(cityId = it, popNavigation = { navController.popBackStack() })
}
}
}
}
我相信这与解决问题无关,但是 Routes.kt 中的代码文件如下:
package com.example.crud.navigation

object Routes {
private const val DETAILS_BASE_ROUTE = "details/"
const val HOME = "home"
const val REGISTER = "register"
const val CITY_ID_KEY = "cityId"
const val DETAILS = "$DETAILS_BASE_ROUTE{$CITY_ID_KEY}"

fun getDetailsDynamicRoute(cityId: Int) = "$DETAILS_BASE_ROUTE${cityId}"
}
我究竟做错了什么?

最佳答案

但我认为你不需要在主类上创建 NavController,因为 la 类 NavigationComponent 的这一部分我认为这个类是负责处理这个类的。
我是 android 世界的新手,但 NavigationComponent 控制 NavController 及其与它的所有交互对我来说很有意义。
通常我是这样用的

@Composable fun NavigationHost() {

val navController = rememberNavController()

MyTheme {
NavHost(
navController = navController,
NavItem.Main.route
)
{

composable(route = NavItem.Main.route) {
FeedScreen { productOnClick ->
navController.navigate(NavItem.Detail.createNavRoute(productOnClick.id))
}
}

composable(route = NavItem.Main.route) {
ConfirmationScreen {
navController.navigate(NavItem.Main.route)
}
}
}

}
}

关于android - 如何将 `rememberNavController` 从喷气背包组合成使用刀柄的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69276165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com