为什么导航器在撰写给出非法参数错误?



对于导航,我有navigation.kt,其中包含用于导航的Nav()。还有一个辅助函数give_route(),如果它是已知的(硬编码),然后给出它的路由,否则page_not_found路由-navController的字符串

package com.<company_name.<app_name>
import androidx.compose.runtime.Composable
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.<company_name.<app_name>

// Class For function needed for Navigation
@Composable
fun Nav() {
val navController = rememberNavController()
NavHost(navController = navController, startDestination = Address.Feed.route) {
composable(Address.Feed.route)
{
feed(
Navi = {add -> navController.navigate(give_route(add))}
)
}
}

}

fun give_route(title: String): String {
return when (title) {
"Feed" -> Address.Feed.route
"Question" -> Address.Question.route
"Notice" -> Address.Notice.route
"Status" -> Address.Status.route
"DM" -> Address.DM.route
"Chat" -> Address.Chat.route
"Group_chat" -> Address.Group_chat.route
"Notify" -> Address.Notify.route
"Search" -> Address.Search.route
"Profile" -> Address.Profile.route
"Profile-Pvt" -> Address.Profile_Pvt.route
"Profile-User" -> Address.Profile_User.route
"Ratting" -> Address.Ratting.route
"New - Text" -> Address.New_Text.route
"New - Image" -> Address.New_Image.route
"New - Video" -> Address.New_Video.route
"New - Audio" -> Address.New_Audio.route
"New - Other" -> Address.New_File.route
else -> Address.page_not_found.route
}
}

所有路由字符串存储在密封类Address.kt

package com.<company_name.<app_name>
sealed class Address(val route: String) {
object Feed : Address("Feed")
object Question : Address("Question")
object Notice : Address("Notice")
object Status : Address("Status")
object DM : Address("DM")
object Chat : Address("Chat")
object Group_chat : Address("Group_chat")
object Notify : Address("Notify")
object Search : Address("Search")
object Profile : Address("Profile")
object Profile_Pvt : Address("Profile-Pvt")
object Profile_User : Address("Profile-User")
object Ratting : Address("Ratting")
object New_Text : Address("New - Text")
object New_Image : Address("New - Image")
object New_Video : Address("New - Video")
object New_Audio : Address("New - Audio")
object New_File : Address("New - File")
object page_not_found : Address("Page not found")

}

我正在运行Nav()从MainActivity.kt

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Nav()
}
}
}
}
}

最后所有的@Composable乐趣。都在Views/Pages.kt。为了这个问题的目的,我隐藏了所有有趣的东西,除了feed和它的Menyitem称为func,例如New_Text

package com.<company_name.<app_name>

import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
// PAGE VIEWS

// Feed View
@Composable
fun feed(Navi: (String) -> Unit) {
var newPost by remember { mutableStateOf(false) } // TODO - y
Row() {
FilledTonalIconButton(
onClick = { newPost = true },
enabled = true
) {
Icon(
Icons.Rounded.Add,
"Button for creating a new post. " +
"Opens the Menu to selct the type for your new post"
)
}
DropdownMenu(
expanded = newPost,
onDismissRequest = { newPost = false }
) {
DropdownMenuItem(
text = { Text("Text") },
onClick = { Navi("New - Text") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.ViewHeadline,
"To Post Text"
)
}
)
DropdownMenuItem(
text = { Text("Image") },
onClick = { Navi("New - Image") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.Wallpaper,
"To Post Image"
)
}
)
DropdownMenuItem(text = { Text("Video") },
onClick = { Navi("New - Video") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.Slideshow,
"To Post Video"
)
}
)
DropdownMenuItem(text = { Text("Audio") },
onClick = { Navi("New - Audio") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.SpatialAudioOff,
"To Post Audio"
)
}
)
DropdownMenuItem(text = { Text("Other") },
onClick = { Navi("New - Other") },
enabled = true,
leadingIcon = {
Icon(Icons.Rounded.PostAdd,
"To open custom file window for posting user file"
)
}
)
}
}
}

@Composable
fun New_Text() {
Text("a")
}

@Composable
fun New_Image() {
Text("b")
}

@Composable
fun New_Video() {
Text("c")
}

@Composable
fun New_Audio() {
Text("d")
}

@Composable
fun New_File() {
Text("e")
}

关于依赖、导入、版本等,请访问我的其他问题

当我在调试设备上运行时,提要正在绘制,下拉菜单在单击时展开,但只要我按下任何菜单按钮,我就会得到以下错误和应用程序停止。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.<company_name.<app_name>, PID: 20633
java.lang.IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/New - Audio } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78c8aacb) route=Feed}
at androidx.navigation.NavController.navigate(NavController.kt:1664)
at androidx.navigation.NavController.navigate(NavController.kt:1984)
at androidx.navigation.NavController.navigate$default(NavController.kt:1979)
at com.<company_name.<app_name>.NavigationKt$Nav$1$1$1.invoke(Navigation.kt:21)
at com.<company_name.<app_name>.NavigationKt$Nav$1$1$1.invoke(Navigation.kt:20)
at com.<company_name.<app_name>.views.PagesKt$feed$1$3$4$1.invoke(Pages.kt:69)
at com.<company_name.<app_name>.views.PagesKt$feed$1$3$4$1.invoke(Pages.kt:69)
at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke-k-4lQ0M(Clickable.kt:153)
at androidx.compose.foundation.ClickableKt$clickable$4$gesture$1$2.invoke(Clickable.kt:142)
at androidx.compose.foundation.gestures.TapGestureDetectorKt$detectTapAndPress$2$1$1.invokeSuspend(TapGestureDetector.kt:222)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:178)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:166)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:397)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:431)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:420)
at kotlinx.coroutines.CancellableContinuationImpl.resumeWith(CancellableContinuationImpl.kt:328)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter$PointerEventHandlerCoroutine.offerPointerEvent(SuspendingPointerInputFilter.kt:566)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.dispatchPointerEvent(SuspendingPointerInputFilter.kt:456)
at androidx.compose.ui.input.pointer.SuspendingPointerInputFilter.onPointerEvent-H0pRuoY(SuspendingPointerInputFilter.kt:469)
at androidx.compose.ui.node.BackwardsCompatNode.onPointerEvent-H0pRuoY(BackwardsCompatNode.kt:394)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:314)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.Node.dispatchMainEventPass(HitPathTracker.kt:301)
at androidx.compose.ui.input.pointer.NodeParent.dispatchMainEventPass(HitPathTracker.kt:183)
at androidx.compose.ui.input.pointer.HitPathTracker.dispatchChanges(HitPathTracker.kt:102)
at androidx.compose.ui.input.pointer.PointerInputEventProcessor.process-BIzXfog(PointerInputEventProcessor.kt:98)
at androidx.compose.ui.platform.AndroidComposeView.sendMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1329)
at androidx.compose.ui.platform.AndroidComposeView.handleMotionEvent-8iAsVTc(AndroidComposeView.android.kt:1275)
at androidx.compose.ui.platform.AndroidComposeView.dispatchTouchEvent(AndroidComposeView.android.kt:1214)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3170)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2846)
at android.view.View.dispatchPointerEvent(View.java:15384)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:6896)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:6667)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6114)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6176)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6137)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:6311)
E/AndroidRuntime:     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6145)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:6368)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6118)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:6176)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:6137)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:6145)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:6118)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:9272)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:9223)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:9172)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:9415)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:276)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:339)
at android.os.Looper.loopOnce(Looper.java:186)
at android.os.Looper.loop(Looper.java:351)
at android.app.ActivityThread.main(ActivityThread.java:8355)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1013)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.ui.platform.MotionDurationScaleImpl@4373ed0, androidx.compose.runtime.BroadcastFrameClock@5894ec9, StandaloneCoroutine{Cancelling}@9edf9ce, AndroidUiDispatcher@fc450ef]
I/Process: Sending signal. PID: 20633 SIG: 9

抱歉发了这么长时间。

编辑:我在这个答案中回答了

和平!

修复错误。有三个错误。错误信息甚至告诉我们错误。

第二行错误输出:

Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/New - Audio } cannot be found in the navigation graph NavGraph(0x0) startDestination={Destination(0x78c8aacb) route=Feed}

这导致我修复第一个错误:

Address.kt中:删除"New - " name"作为路由,使用"New_name"。
object New_Image : Address("New - Image")object New_Image : Address("New_Image")

然后我发现

Navhost中,我没有为那些在feed视图中从下拉菜单中被要求的页面创建可组合功能

最后一个错误是它正在创建不正确的构建,重新启动时解决。我怀疑这是因为缺乏Ram,因为在关闭Android Studio之前,它占用了5个内存。GB和Chrome ~我的8GB笔记本电脑的2GB,但它可能是其他任何东西。

但我的另一个问题仍然没有解决。请看一看。链接:

最新更新