导航的默认空值组件:参数过多错误

时间:2020-05-17 13:18:15

标签: android android-fragments

我正在从片段中实现日期选择器。我想在片段上显示选定的日期。

日期选择器对话框片段:

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返回并将其显示为Toast时,我需要它。因此,如您所见,我为参数添加了空值: app:nullable =“ true” android:defaultValue =“ @ null” 在导航图中。但是问题是,编译器显示错误:

用于公共开放乐趣actionDatePickerDialogFragmentToDatePickerDemo的参数过多

没有默认值,程序正在编译,但由于错误而崩溃:

 java.lang.IllegalArgumentException: Required argument "year" is missing and does not have an android:defaultValue

这很明显。那么解决方案是什么?

注意到通过导航库从片段到dilog进行通信时,会发生这种问题。

1 个答案:

答案 0 :(得分:0)

如果参数类型支持空值,则可以声明一个默认值 使用android:defaultValue =“ @ null”

的null值
<argument
            android:name="customer"
            app:argType="com.test.CustomerEntity"
            app:nullable="true"
            android:defaultValue="@null"/>

在出现的“添加自变量链接”窗口中,输入自变量名称, 参数类型,参数是否可为null(app:nullable =“ true”), 和默认值(如果需要)。

https://developer.android.com/guide/navigation/navigation-pass-data