Android 在Kotlin中使用safeArgs导航会引发错误
我有一个片段,可以通过底部导航栏导航到,不带参数,也可以从另一个带参数的片段导航到。导航本身工作得非常好,但一旦我添加参数,它就会崩溃 我不确定需要多少代码,因此如果代码不完整,很抱歉: 这是我的导航片段:Android 在Kotlin中使用safeArgs导航会引发错误,android,kotlin,android-safe-args,Android,Kotlin,Android Safe Args,我有一个片段,可以通过底部导航栏导航到,不带参数,也可以从另一个带参数的片段导航到。导航本身工作得非常好,但一旦我添加参数,它就会崩溃 我不确定需要多少代码,因此如果代码不完整,很抱歉: 这是我的导航片段: <navigation xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/navigation"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="fh.wfp2.flatlife.ui.views.HomeFragment"
android:label="Flatlife"
tools:layout="@layout/home_fragment" />
<fragment
android:id="@+id/todoFragment"
android:name="fh.wfp2.flatlife.ui.views.TodoFragment"
android:label="Todos">
<action
android:id="@+id/action_todoFragment_to_addTodoFragment"
app:destination="@id/addTodoFragment" />
<argument
android:name="taskname"
app:argType="string" />
<argument
android:name="isImportant"
app:argType="boolean" />
</fragment>
<fragment
android:id="@+id/addTodoFragment"
android:name="fh.wfp2.flatlife.ui.views.AddTodoFragment"
android:label="AddTodoFragment">
<action
android:id="@+id/action_addTodoFragment_to_todoFragment"
app:destination="@id/todoFragment" />
</fragment>
</navigation>
这就是我试图在TodoFragment中获取参数的方法
private val args: TodoFragmentArgs by navArgs()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//setting binding and other onClickListeners but they work fine
//reading args
//this i thought would work because then args only get read if not null
args?.let { //debugger stops here and app crashes
if(args.taskname.isNotEmpty())
viewModel.onAddTodoClick(Todo(name = args.taskname, isImportant = args.isImportant))
}
/*second try which I took directly from developers.android but which doesn't make sense in my case i
think because the args could be null I guess
val todoName = args.taskname
val isImportant = args.isImportant
viewModel.onAddTodoClick(Todo(name = todoName, isImportant = isImportant))
*/
}
这是我收到的错误消息:
我希望我的意思很清楚,否则,我会更新这个问题。这可能是件简单的事,但我不太明白
val args = arguments?.let {
SecondFragmentArgs.fromBundle(
it
)
}
if (args != null) {
firstDataList = args.taskName
secondDataList = args.isImportant
}
这就是接收片段代码的样子。这应该行得通。所以要感谢@Onik让SafeArgs离开的想法。我需要转储SafeArgs类调用以接收参数,现在看起来是这样的。我想这肯定不是最干净的方式,但我还不知道为什么SafeArgs调用不起作用,而且我对Kotlin来说太新了,无法以更干净的方式编写以下代码。但它现在起作用了
arguments?.let {
var myArg1: String? = null
var myArg2: Boolean
arguments?.getString("argument1")?.let { arg1->
myArg1= arg1
}
arguments?.getBoolean("argument2")?.let { arg2->
myArg2= arg2
if (myArg1!= null)
//do logis with args
}
}
findNavController().navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment(taskName,IsTemperatant)这应该是发送代码。我尝试了这个方法,但没有成功。我仍然得到相同的错误代码。(我将更新帖子,以便错误代码可见)你检查发送代码了吗?是的,我检查了。我使用绑定,但语法与我使用的相同。但是,当导航到未设置参数的片段时,应用程序崩溃,因此,如果有意义,我甚至无法访问发送参数的片段。“仅当导航到TodoFragment而未传递参数时。”我不明白这是怎么可能的。代码无法编译,因为该操作需要2个参数。@Onik引用的操作在场景中没有任何参数就无法导航。在没有参数的情况下导航时,我从HomeFragment->TodoFragment(带底部导航)转到当使用参数导航时,我从AddTodoFragment->TodoFragment开始。我相信这就是问题所在。当使用底部导航时调用
args?.let
时,args
被惰性初始化,并引发异常。@Onik-hm-ok。但是随着下面答案中PiyushMaheswari的变化,它应该会起作用,r是吗?因为我想这会立即初始化?我会再试一次,但之前它不起作用。@Onik谢谢!!这对我来说很有效!我这边还有一些关于逻辑的问题,但你的tipp解决了这个问题。非常感谢!如果你想制定一个答案,我会接受它,否则我会更新我的问题你知道为什么这样做有效还是用更好的措辞:为什么TodoFragmentArgs不起作用?
arguments?.let {
var myArg1: String? = null
var myArg2: Boolean
arguments?.getString("argument1")?.let { arg1->
myArg1= arg1
}
arguments?.getBoolean("argument2")?.let { arg2->
myArg2= arg2
if (myArg1!= null)
//do logis with args
}
}