'Unresolved reference: NavArgs after added some arguments to destination
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
Solution 1:[1]
As per the documentation on that very page:
When using the
-ktxdependencies, 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"
Solution 2:[2]
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" />
Solution 3:[3]
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.
Solution 4:[4]
i had the same problem and i solved this by doing following steps
Build -> Clean Project- delete argument from
nav_graph.xmlfile - create new argument
Build -> Rebuild Project- set the argument
val action: NavDirections = AreThereAnyDecayedTeethInTheAreaOfPainFragmentDirections
.actionAreThereAnyDecayedTeethInTheAreaOfPainFragmentToResultFragment(
finalresult = "somethings"
)
- use this argument on FragmentDest
arguments.let {
binding.board.text = ResultFragmentArgs.fromBundle(it!!).finalresult
}
Solution 5:[5]
I solved this by doing Clean Project from Build menu of Android Studio.
Go to Menu : Build >> Clean Project
Solution 6:[6]
just fyi if you want to pass String your argType should be string instead of String.
app:argType="string"
Solution 7:[7]
In my case I need to added this it in my build.gradle (app)
kotlinOptions {
jvmTarget = '1.8'
}
Solution 8:[8]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
