Ioa*_* P. 20 android kotlin android-jetpack-compose jetpack-compose-navigation
我有一个导航图,如下所示:
@Composable
fun NavGraph (
navController: NavHostController
) {
NavHost(
navController = navController,
startDestination = "Products"
) {
composable(
route = "Products"
) {
ProductsScreen(
navController = navController
)
}
composable(
route = "Product Details",
arguments = listOf(
navArgument("product") {
type = NavType.SerializableType(Product::class.java)
}
)
) {
val product = navController.previousBackStackEntry?.arguments?.getSerializable("product") as Product
ProductDetailsScreen(
navController = navController,
product = product
)
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 ProductDetailsScreen 内,我希望单击产品以进一步导航到传递 Product 对象的详细信息屏幕:
LazyColumn {
items(
items = products
) { product ->
ProductCard(
product = product,
onProductClick = {
navController.currentBackStackEntry?.arguments?.putSerializable("product", product)
navController.navigate("Product Details")
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
产品完美显示,但当我单击产品时,应用程序崩溃并出现以下错误:
java.lang.IllegalArgumentException:在导航图中找不到与请求 NavDeepLinkRequest{ uri=android-app://androidx.navigation/Product Details } 匹配的导航目的地 NavGraph(0x0) startDestination={Destination(0xb543811) route=Products}
有人可以帮忙吗?
PS我也遵循了这个答案,但没有运气:(
gtx*_*eme 13
确保所有参数名称和路由名称正确设置并避免空格,当您想要断词时使用下划线
我的错
路由定义中的参数名称和navArgument
块中提供的参数名称应该相同
composable(
"pokemon_detail_screen/{dominantColor}/{pokemonName}", //Notice over here
arguments = listOf(
navArgument("dominantColor") {
type = NavType.IntType
},
navArgument("pokemon_name") { // Notice over here
type = NavType.StringType
}
)
)
Run Code Online (Sandbox Code Playgroud)
我花了很长时间才弄清楚所以要非常小心
Aqu*_*hka 11
当我使用 json 作为字符串将类传递给导航时出现此错误,这是我的实现方式:
composable(
"details/{MyObject}",
arguments = listOf(navArgument("MyObject") {
type = NavType.StringType
})
) {
it.arguments?.getString("MyObject")?.let { jsonString ->
var issue = MyObject()
if (!jsonString.isNullOrEmpty())
issue = jsonString.fromJson(MyObject::class.java)
DetailsView(navController = navController, detailsViewModel, myObject = MyObject)
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么称呼它:
val gson = Gson()
var myObjectString = gson.toJson(myObject, MyObject::class.java)
navController.navigate("details/$myObjectString")
Run Code Online (Sandbox Code Playgroud)
有时我会收到Navigation destination that matches request NavDeepLinkRequest cannot be found in the navigation graph NavGraph
错误,有时却没有收到错误。这取决于我点击的对象。
我认为这是因为当我从 api 得到答案时,我的一些数据格式不正确,这意味着当我将其转换为 json 时,我可能会有一些不需要的字符。为了避免这种情况,我将 json 编码为 utf8,如下所示:
var encode = URLEncoder.encode(myObjectString,StandardCharsets.UTF_8.toString())
navController.navigate("details/$encode")
Run Code Online (Sandbox Code Playgroud)
然后我没有再次收到导航错误,我希望这可以帮助那些尝试使用 jetpack 组合导航和 json 发送对象的人
小智 0
这是我的样品
@ExperimentalPagerApi
@Composable
fun WallNavigation(nameFolder: String, paths: List<String>) {
val navController = rememberNavController()
val model: ImageViewModel = viewModel()
model.setFolder(nameFolder)
model.setFileNames(paths)
NavHost(navController = navController, startDestination = Screen.MainScreen.route) {
composable(Screen.MainScreen.route) {
MainScreen(navController = navController, model)
}
composable(route = Screen.GalleryScreen.route + "/{position}", arguments = listOf(navArgument(name = "position") {
type = NavType.IntType
defaultValue = -1
nullable = false
})) { entity ->
GalleryScreen(navController = navController, position = entity.arguments?.getInt("position") ?: -1, imageViewModel = model)
}
composable(route = Screen.CropScreen.route) {
CropScreen(navController = navController, imageViewModel = model)
}
}
}
sealed class Screen(val route : String) {
object MainScreen : Screen("main_screen")
object GalleryScreen : Screen("gallery_screen")
object CropScreen : Screen("crop_screen")
fun withArgs(vararg args: String):String {
return buildString {
append(route)
args.forEach { arg ->
append("/$arg")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23762 次 |
最近记录: |