[英]Navigating with safeArgs in Kotlin throws and error
I have a fragment that can be either navigated to via a bottom navBar without arguments or from a different fragment with arguments.我有一个片段,可以通过没有 arguments 的底部导航栏导航到,也可以从带有 arguments 的不同片段导航到。 The navigation itself works perfectly fine but as soon as I add my arguments it crashes.
导航本身工作得很好,但只要我添加我的 arguments 它就会崩溃。
I'm unsure how much code is needed, so sorry if it's not complete:我不确定需要多少代码,如果不完整,请见谅:
This is my navigation fragment:这是我的导航片段:
<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>
This is my addTodoFragment :这是我的addTodoFragment :
class AddTodoFragment : Fragment(R.layout.add_task_fragment) {
private lateinit var binding: AddTaskFragmentBinding
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = AddTaskFragmentBinding.bind(view)
binding.bAddTodo.setOnClickListener {
if (binding.etAddTodo.text.isNotEmpty()) {
// viewModel.onAddTodoClicked(binding.etAddTodo.text.toString(),
binding.cbImportant.isChecked)
findNavController().navigate(
AddTodoFragmentDirections.actionAddTodoFragmentToTodoFragment(
binding.etAddTodo.text.toString(), binding.cbImportant.isChecked
)
)
} else {
Snackbar.make(it, "The task field can't be empty", Snackbar.LENGTH_SHORT).show()
}
}
}
}
This is how I'm trying to get the arguments in TodoFragment .这就是我试图在 TodoFragment 中获取arguments的方式。
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))
*/
}
This is the error message i get:这是我收到的错误消息:
I hope it is clear what I mean, otherwise, I'll update the question.我希望我的意思很清楚,否则,我会更新问题。 It'll probably be something simple but I can't quite put my finger on it.
这可能会很简单,但我不能完全确定它。
val args = arguments?.let {
SecondFragmentArgs.fromBundle(
it
)
}
if (args != null) {
firstDataList = args.taskName
secondDataList = args.isImportant
}
This is how the receiving fragment code should look like.这就是接收片段代码的样子。 This should work.
这应该有效。
So credit to @Onik for the idea to leave SafeArgs away.所以感谢@Onik 离开SafeArgs 的想法。 I needed to dump the SafeArgs class call for receiving the arguments and it looks like this now.
我需要转储 SafeArgs class 调用以接收arguments,它现在看起来像这样。 This is definitely not the cleanest way I guess but I don't know yet why the SafeArgs call doesn't work and I'm too new to Kotlin to write the following code in a cleaner way.
这绝对不是我猜想的最干净的方式,但我还不知道为什么 SafeArgs 调用不起作用,而且我对 Kotlin 太陌生,无法以更干净的方式编写以下代码。 But it works, for now.
但它现在有效。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.