Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

androidx Navigation: Too many arguments for @NonNull public open fun

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
like image 990
Ananth Avatar asked Jul 14 '19 16:07

Ananth


2 Answers

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.

like image 90
Ananth Avatar answered Sep 17 '22 16:09

Ananth


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".

like image 35
Mayu Avatar answered Sep 16 '22 16:09

Mayu