Unresolved reference: NavArgs after added some arguments to destination
Asked Answered
A

12

26

I'm working on a small project and trying to use the new navigation architecture components. When i'm trying to add some arguments to a destination i got "Unresolved reference: NavArgs" error.

I followed this guide https://developer.android.com/topic/libraries/architecture/navigation/navigation-pass-data#kotlin and already added

classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha11"

to my project gradle file and also added

apply plugin: 'androidx.navigation.safeargs.kotlin'

to my app gradle file.

As seen in the guide above i want to use val args: AddKittenFragmentArgs by navArgs() to get the passed arguments. But navArgs() isn't recognized.

Also NavArgs in the generated code isn't resolved.

data class MyFragmentArgs(val argOne: String? = "\"\"", val argTwo: String? = "\"\"") : NavArgs
Abampere answered 24/1, 2019 at 19:29 Comment(0)
P
12

As per the documentation on that very page:

When using the -ktx dependencies, Kotlin users can also use the by navArgs() property delegate to access arguments.

Make sure you are following the Adding Components documentation and using the navigation-fragment-ktx dependency:

implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha11"
Picard answered 24/1, 2019 at 21:53 Comment(0)
P
18

In my case i typed the argument Name starting by a capital letter

        <argument
            android:name="MyArgument" // changed it to myArgument fix the problem
            app:argType="string"
            app:nullable="false" />
Puppetry answered 21/8, 2021 at 13:1 Comment(0)
P
12

As per the documentation on that very page:

When using the -ktx dependencies, Kotlin users can also use the by navArgs() property delegate to access arguments.

Make sure you are following the Adding Components documentation and using the navigation-fragment-ktx dependency:

implementation "android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha11"
Picard answered 24/1, 2019 at 21:53 Comment(0)
C
11

i had the same problem and i solved this by doing following steps

  1. Build -> Clean Project
  2. delete argument from nav_graph.xml file
  3. create new argument
  4. Build -> Rebuild Project
  5. set the argument
val action: NavDirections = AreThereAnyDecayedTeethInTheAreaOfPainFragmentDirections
                .actionAreThereAnyDecayedTeethInTheAreaOfPainFragmentToResultFragment(
                    finalresult = "somethings"
                )
  1. use this argument on FragmentDest
        arguments.let {
            binding.board.text = ResultFragmentArgs.fromBundle(it!!).finalresult
        }
Correggio answered 23/1, 2021 at 13:16 Comment(0)
N
6

I had the same problem until I realized the navigation component's project dependencies were using a different version than the one specified by the plugin (in the classpath).

i.e. In the project's build.gradle

classpath 'android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-beta01'

in the app build.gradle

// Navigation
implementation 'android.arch.navigation:navigation-fragment-ktx:'+ rootProject.navigationVersion
implementation 'android.arch.navigation:navigation-ui-ktx:' + rootProject.navigationVersion

where navigationVersion was

ext {
   ...
   navigationVersion = "1.0.0-alpha08"
}

lint doesn't tell you there's an update to a library when the dependency is interpolated.

Naarah answered 10/2, 2019 at 21:43 Comment(0)
W
5

I solved this by doing Clean Project from Build menu of Android Studio.

Go to Menu : Build >> Clean Project

Wanda answered 28/6, 2019 at 7:34 Comment(1)
I also had all the solutions mentioned above in place thought this is hat solved my problem.Prado
S
4

Had the same issue I used "setStartDestination(.." and now it works, instead of using the property access syntax i.e "startDestination=..."

Sliding answered 2/1, 2023 at 7:30 Comment(3)
Same with me i used setStartDestination() instead of startDestination it works.Fortyfive
I bumped the navigation library from 2.3.0 to 2.5.0 and had to replace the property access syntax to use the setter function and started getting "Unresolved reference: FooBarActivityArgs"Transpacific
@GauriGadkari were you able to resolve it?Sliding
M
1

In my case I need to added this it in my build.gradle (app)

kotlinOptions {
    jvmTarget = JavaVersion.VERSION_1_8.toString()
}
Macronucleus answered 26/5, 2021 at 16:57 Comment(0)
P
0

Latest release version 1.0.0 seems to have fixed this issue. Just change the navigation dependency version to 1.0.0 along with adding the -ktx dependencies as mentioned in other answers and everything should work.

Purview answered 7/4, 2019 at 6:49 Comment(0)
H
0

just fyi if you want to pass String your argType should be string instead of String.

 app:argType="string"
Highflier answered 8/2, 2020 at 11:25 Comment(2)
Was the the issue for OP?Gates
@Gates No, but it can be an issue for someone elseHighflier
A
0

Step 1: Make it sure that your AndroidManifest.xml is containing package name like that -

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.app">

Step 2: Use the latest version from here.

Step 3: Clean build the project.

Step 4: Rebuild the project.

Acetaldehyde answered 5/6, 2022 at 19:4 Comment(0)
W
0

I also faced this issue adding id 'androidx.navigation.safeargs.kotlin' this plugin in the corresponding gradle file fixed the issue. We need to clean build once this plugin is added.

Weightless answered 13/12, 2022 at 8:34 Comment(0)
F
0

For me the problem was solved when I inserted the code inside fragment class:

class UpdateNoteFragment: Fragment(R.layout.fragment_update_note) {
   lateinit var binding: FragmentUpdateNoteBinding
   lateinit var viewModel: NotasViewModel
   lateinit var notaSeleccionada:Nota
   val args:UpdateNoteFragmentArgs by navArgs()
   ....

Hope it helps!

Fiesole answered 12/2 at 12:13 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Chela

© 2022 - 2024 — McMap. All rights reserved.