[Android] Safe Args - Too many arguments for public open fun 에러

유정현·2024년 1월 3일
0

다음과 같은 에러가 발생했다.
Too many arguments for public open fun nextAction(): BlankFragmentDirections.NextAction defined in com.example.navigationgraph.BlankFragmentDirections

코드는 다음과 같았다.

<fragment
        android:id="@+id/blankFragment02"
        android:name="com.example.navigationgraph.BlankFragment2"
        android:label="fragment_blank2"
        tools:layout="@layout/fragment_blank2">
        <argument
            android:name="blank_arg_number"
            app:argType="string"
            android:defaultValue="hi"
            />
        <action
            android:id="@+id/before_action"
            app:destination="@+id/blankFragment"

            app:enterAnim="@anim/slide_in_left"
            app:exitAnim="@anim/slide_out_right"
            app:popEnterAnim="@anim/slide_in_right"
            app:popExitAnim="@anim/slide_out_left"/>
    </fragment>
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.firstButton.setOnClickListener {
            findNavController().navigate(BlankFragmentDirections.nextAction("hello"))
        }
    }

네.. 또 stackoverflow에서 도움을 얻었습니다..
https://stackoverflow.com/questions/67414621/i-cannot-set-safe-args-default-value

If your fragment parameter has a default value the generated builder method will not have parameter for it. You will need to set the value of that fragment parameter separately on the xDirections object.

default value 값이 지정되어 있으면 builder method가 parameter를 갖지 않게 된다고 합니다.
그래서 다음과 같이 수정했습니다.

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.firstButton.setOnClickListener { findNavController().navigate(BlankFragmentDirections.nextAction().apply {
                blankArgNumber = "hello"
            })
        }
    }

0개의 댓글

관련 채용 정보