为什么在导航中切换屏幕时不能将 null 作为参数?
Why can't I get null as an argument when switching screens in navigation?
我用navigation
和SafeArgs
到switch screens
和pass data
。
在屏幕上选择数据时A
,数据在屏幕切换的同时传输。
但是,有时屏幕 A
会在没有任何操作的情况下切换到屏幕 B
。
我决定在这种情况下发送null
。
但我不断收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lightweight, PID: 9555
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54)
at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35)
at com.example.lightweight.fragment.WriteRoutineFragment.getArgs(WriteRoutineFragment.kt:20)
at com.example.lightweight.fragment.WriteRoutineFragment.onViewCreated(WriteRoutineFragment.kt:44)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2985)
navigation.xml
<fragment
android:id="@+id/writeRoutine"
android:name="com.example.lightweight.fragment.WriteRoutineFragment"
android:label="fragment_write_routine"
tools:layout="@layout/fragment_write_routine" >
<action
android:id="@+id/action_writeRoutineFragment_to_workoutListTabFragment"
app:destination="@id/workoutListTabFragment" />
<argument
android:name="workout"
app:argType="string"
app:nullable="true" />
</fragment>
在适配器中
inner class ViewHolder(itemView: View, context: Context) : RecyclerView.ViewHolder(itemView) {
private val tv: TextView = itemView.findViewById(R.id.workout)
init {
tv.setOnClickListener { view ->
val workout = tv.text.toString()
val action: NavDirections = WorkoutListTabFragmentDirections.actionWorkoutListTabToWriteRoutine(workout)
view.findNavController().navigate(action)
}
}
fun bind(item: String) {
tv.text = item
}
}
这是什么原因?
您应该添加解决方法参数的默认值。这里因为设置了app:nullable="true"
,所以应该设置android:defaultValue="@null"
.
修改创建解决方法参数的方式如下:
<argument
android:name="workout"
app:argType="string"
android:defaultValue="@null"
app:nullable="true" />
我用navigation
和SafeArgs
到switch screens
和pass data
。
在屏幕上选择数据时A
,数据在屏幕切换的同时传输。
但是,有时屏幕 A
会在没有任何操作的情况下切换到屏幕 B
。
我决定在这种情况下发送null
。
但我不断收到以下错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.lightweight, PID: 9555
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54)
at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35)
at com.example.lightweight.fragment.WriteRoutineFragment.getArgs(WriteRoutineFragment.kt:20)
at com.example.lightweight.fragment.WriteRoutineFragment.onViewCreated(WriteRoutineFragment.kt:44)
at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2985)
navigation.xml
<fragment
android:id="@+id/writeRoutine"
android:name="com.example.lightweight.fragment.WriteRoutineFragment"
android:label="fragment_write_routine"
tools:layout="@layout/fragment_write_routine" >
<action
android:id="@+id/action_writeRoutineFragment_to_workoutListTabFragment"
app:destination="@id/workoutListTabFragment" />
<argument
android:name="workout"
app:argType="string"
app:nullable="true" />
</fragment>
在适配器中
inner class ViewHolder(itemView: View, context: Context) : RecyclerView.ViewHolder(itemView) {
private val tv: TextView = itemView.findViewById(R.id.workout)
init {
tv.setOnClickListener { view ->
val workout = tv.text.toString()
val action: NavDirections = WorkoutListTabFragmentDirections.actionWorkoutListTabToWriteRoutine(workout)
view.findNavController().navigate(action)
}
}
fun bind(item: String) {
tv.text = item
}
}
这是什么原因?
您应该添加解决方法参数的默认值。这里因为设置了app:nullable="true"
,所以应该设置android:defaultValue="@null"
.
修改创建解决方法参数的方式如下:
<argument
android:name="workout"
app:argType="string"
android:defaultValue="@null"
app:nullable="true" />