我正在使用导航组件,但我不明白为什么如果定义了参数,则将参数传递给下面的方法时会出现错误。我正在使用 SafeArgs,只有当我为此参数定义默认值时才会出现此错误。
有人可以解释一下为什么会发生这种情况以及如何解决它吗?
这是导航图的部分代码。
<fragment
android:id="@+id/sleepTrackerFragment"
android:name="com.example.sleeptracker.fragments.sleep_tracker.SleepTrackerFragment"
android:label="fragment_sleep_tracker"
tools:layout="@layout/fragment_sleep_tracker" >
<action
android:id="@+id/action_sleepTrackerFragment_to_sleepQualityFragment"
app:destination="@id/sleepQualityFragment" />
<argument
android:name="qualityLastSleep"
app:argType="integer"
android:defaultValue="-1" />
</fragment>
<fragment
android:id="@+id/sleepQualityFragment"
android:name="com.example.sleeptracker.fragments.sleep_quality.SleepQualityFragment"
android:label="fragment_sleep_quality"
tools:layout="@layout/fragment_sleep_quality" >
<action
android:id="@+id/action_sleepQualityFragment_to_sleepTrackerFragment"
app:destination="@id/sleepTrackerFragment" >
</action>
</fragment>
Run Code Online (Sandbox Code Playgroud) 我正在使用 ViewBinding,并且尝试减少创建 Fragment 的代码,该 Fragment 是一个抽象类并包含以下代码:
abstract class MyFragment<T> : Fragment() {
private var binding: T? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = getBinding()
return binding.root
}
abstract fun getBinding(): T
}
Run Code Online (Sandbox Code Playgroud)
为了使其工作,我需要让 T 扩展一个类,并且该类需要是所有绑定类的父类。
所有生成的绑定类都有一个共同的父类?如果是这样那是什么?