我正在从我的片段中实现一个日期选择器。我想在我的片段中显示选定的日期。
日期选择器对话框片段:
class DatePickerDialogFragment : DialogFragment() , DatePickerDialog.OnDateSetListener {
@NotNull
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//return super.onCreateDialog(savedInstanceState)
// Use the current date as the default date in the picker.
val c: Calendar = Calendar.getInstance()
val year: Int = c.get(Calendar.YEAR)
val month: Int = c.get(Calendar.MONTH)
val day: Int = c.get(Calendar.DAY_OF_MONTH)
// Create a new instance of DatePickerDialog and return it.
return DatePickerDialog(requireActivity(), this, year, month, day)
}
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
val selectedDate = arrayOf<String>(year.toString(),month.toString(),dayOfMonth.toString())
val action = DatePickerDialogFragmentDirections.actionDatePickerDialogFragmentToDatePickerDemo(month.toString(),year.toString(),dayOfMonth.toString())
//Navigation.findNavController().navigate(action)
findNavController().navigate(action)
}
}
DatePickerDemo类:
class DatePickerDemo : Fragment() ,View.OnClickListener {
private var mSelectedMonth: String? = null
private var mSelectedDay: String? = null
private var mSelectedYear: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_date_picker, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btnShowDate.setOnClickListener(this)
arguments?.let {
mSelectedMonth = DatePickerDemoArgs.fromBundle(it).month.toString()
mSelectedDay = DatePickerDemoArgs.fromBundle(it).day.toString()
mSelectedYear = DatePickerDemoArgs.fromBundle(it).year.toString()
}
if(mSelectedMonth != null && mSelectedDay != null && mSelectedYear != null){
processDatePickerResult(mSelectedYear!!.toInt(),mSelectedMonth!!.toInt(),mSelectedDay!!.toInt())
}
}
override fun onClick(v: View?) {
findNavController().navigate(R.id.datePickerDialogFragment)
}
}
片段和对话框片段的导航图如下所示:
<fragment
android:id="@+id/datePickerDemo"
android:name="com.kgandroid.droidcafeuimaster.DatePickerDemo"
android:label="DatePickerDemo" >
<action
android:id="@+id/action_datePickerDemo_to_datePickerDialogFragment"
app:destination="@id/datePickerDialogFragment" />
<argument
android:name="year"
app:argType="string"
app:nullable="true"
android:defaultValue="@null" />
<argument
android:name="month"
app:argType="string"
app:nullable="true"
android:defaultValue="@null" />
<argument
android:name="day"
app:argType="string"
app:nullable="true"
android:defaultValue="@null" />
</fragment>
<dialog
android:id="@+id/datePickerDialogFragment"
android:name="com.kgandroid.droidcafeuimaster.DatePickerDialogFragment"
android:label="fragment_date_picker_dialog"
tools:layout="@layout/fragment_date_picker_dialog" >
<action
android:id="@+id/action_datePickerDialogFragment_to_datePickerDemo"
app:destination="@id/datePickerDemo" />
</dialog>
问题在于:
很明显,当DatePikerDemo片段启动时,我不需要其中的参数。当我从DatePickerDialogFragment返回并将其显示为吐司时,我需要这些参数。因此,我向参数添加了null值,如您所见:**app:nullable=“true”android:defaultValue="@null”**在导航图中。但问题是,编译器显示错误:
公共开放式功能操作DatePickerDialogFragmentToDatePickerDemo的参数太多
如果没有默认值,程序将进行编译,但会崩溃并显示错误:
java.lang.IllegalArgumentException: Required argument "year" is missing and does not have an android:defaultValue
这是很明显的。那么这个问题的解决方案是什么呢?
注意到在通过导航库从片段到日志以及从日志到片段的通信时会出现此类问题。
2条答案
按热度按时间vatpfxk51#
你的论点很好(示例):
看起来您可能在gradle.build文件中为
sageargs
声明了错误的插件。您正在使用Kotlin,因此请使用Kotlin插件。下面是对解决方案的引用:https://stackoverflow.com/a/60941161/619673
简而言之:
转到您的build.gradle文件,从顶部开始更改其中一个插件。
(
androidx.navigation.safeargs
-〉androidx.navigation.safeargs.kotlin
):它应该是:
并确保您使用的是最新版本的库:https://developer.android.com/jetpack/androidx/releases/navigation#2.5.1
参考:https://stackoverflow.com/a/69638315/619673
hwamh0ep2#
如果参数类型支持空值,您可以使用android:defaultValue="@null”声明默认值为空
在出现的“添加参数链接”窗口中,输入参数名称、参数类型、参数是否可为空(app:nullable=“true”)以及默认值(如果需要)。
https://developer.android.com/guide/navigation/navigation-pass-data