12

I am using Navigation Editor in Android Studio to implement Safe Args.

I am accepting arguments in one fragment as

 <fragment
    android:id="@+id/gameWonFragment"
    android:name="com.example.android.navigation.GameWonFragment"
    android:label="@string/android_trivia"
    tools:layout="@layout/fragment_game_won">

    <action
        android:id="@+id/action_gameWonFragment_to_gameFragment"
        app:destination="@id/gameFragment"
        app:popUpTo="@+id/titleFragment">
    </action>
    <argument
        android:name="numQuestions"
        app:argType="integer"
        android:defaultValue="0" />
    <argument
        android:name="numCorrect"
        app:argType="integer"
        android:defaultValue="0" />
 </fragment>

And in my Fragment, I am sending arguments as

view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment(numQuestions, questionIndex))

But, GameFragmentDirections.actionGameFragmentToGameWonFragment() doesn't want to accept arguments. I tried Clean Project and Rebuild Project.

This is throwing:

Too many arguments for @NonNull public open fun actionGameFragmentToGameWonFragment(): GameFragmentDirections.ActionGameFragmentToGameWonFragment defined in com.example.android.navigation.GameFragmentDirections
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ananth
  • 2,597
  • 1
  • 29
  • 39

6 Answers6

31

This issue is fixed by removing the android:defaultValue tag.

So -

<argument
    android:name="numQuestions"
    app:argType="integer"/>
<argument
    android:name="numCorrect"
    app:argType="integer"/>

Then Rebuild Project after making changes.

Ananth
  • 2,597
  • 1
  • 29
  • 39
14

This was answered in a similar post here - https://stackoverflow.com/a/60807951/11615237

Basically, you probably need to use the Kotlin version of the plugin like this apply plugin: "androidx.navigation.safeargs.kotlin".

Mayu
  • 161
  • 1
  • 5
14

I have also run into this issue. I was able to retain the android:defaultValue argument tag and clear up the "too many arguments" error by obtaining an instance of the appropriate action object, setting the necessary values, and then sending that to the navigate command.

So, for example, this:

val actionDetail = GameFragmentDirections.actionGameFragmentToGameWonFragment()
actionDetail.numQuestions = numQuestions
actionDetail.numCorrect = questionIndex
view.findNavController().navigate(actionDetail)

rather than this:

view.findNavController().navigate(GameFragmentDirections.actionGameFragmentToGameWonFragment(numQuestions, questionIndex))
Ananth
  • 2,597
  • 1
  • 29
  • 39
CR5
  • 140
  • 1
  • 3
4

" Too many arguments for public fun View.findNavController(): NavController defined in androidx.navigation "

I encountered the same error.

The confirmed answer didn't solve my problem.

My solution

File -> Invalidate Caches/Restart

Attention

Note that the correct arguments are passed to the correct fragment.

Sample

view.findNavController()
      .navigate(GameFragmentDirections
            .actionGameFragmentToGameWonFragment(numQuestions, questionIndex))

Note that the correct fragment is in action.

Thanks.

Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
  • This solution worked for me. When adding the plugins in the gradle files, android studio didn't recognize they got updated. Manually syncing and building the project fixed this for me. Thank you mate – Daniel Sep 14 '20 at 11:06
2

I faced the same issue. I fixed it by putting arguments inside the action tag.

<action
    android:id="@+id/action_gameWonFragment_to_gameFragment"
    app:destination="@id/gameFragment"
    app:popUpTo="@+id/titleFragment">
    <argument
        android:name="numQuestions"
        app:argType="integer" />
    <argument
        android:name="numCorrect"
        app:argType="integer" />
</action>
Pooja
  • 2,417
  • 20
  • 39
0

I have faced with the same problem. I am using multi backstack navigation. Each tab has its own graph. In one graph needed destination was absent, what caused the problem. And I solved that by duplicating the destination

Adislan
  • 1
  • 1
  • 2