Android : `androidx.navigation.NavArgs` not found
Asked Answered
B

7

9

I fetch this below code from From Codelab for navigation controller

But getting this below error: spend already more the 3 hours but not any success. Here is my error

Please help me to solve this error.

Branchiopod answered 2/3, 2019 at 10:23 Comment(1)
DeepLinkFragmentArgs FlowStepFragmentArgs FlowStepFragmentDirections this all are auto generated class.Branchiopod
N
17

Change this

  implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.0-alpha01'
  implementation 'android.arch.navigation:navigation-ui-ktx:2.2.0-alpha01'

to this:

  implementation 'androidx.navigation:navigation-fragment-ktx:2.2.0-alpha01'
  implementation 'androidx.navigation:navigation-ui-ktx:2.2.0-alpha01'

Notice that android.arch.navigation is replaced with androidx.navigation

also, make sure to apply this plugin on top of build.gradle:

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

and this to the dependencies to the project level build.gradle:

 classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-rc01"
Nones answered 1/10, 2019 at 8:40 Comment(0)
A
12

If you are using kotlin and androidX than replace this

apply plugin: "androidx.navigation.safeargs"

with

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

and in project level build.gradle

dependencies {
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha01"
}

on app level build.gradle

def nav_version = "2.1.0-alpha01"

implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version" 

If you are using kotlin Ktx

implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Hope this will help other people also. It helps me too

Ardenia answered 28/3, 2019 at 6:40 Comment(0)
R
1

Another possible case:

Check in your code that you have actually defined argument tag in the xml file (sender && receiver).

In another case, Args will not be generated.

Following code will not generate Args:

<fragment
    android:id="@+id/receiverFragment"
    android:name="com.example.app.ReceiverFragment"
    android:label="ReceiverFragment" >
</fragment>

Following code will generate Args:

<fragment
    android:id="@+id/receiverFragment"
    android:name="com.example.app.ReceiverFragment"
    android:label="ReceiverFragment" >
    <argument
        android:name="myArgument"
        app:argType="integer"
        app:nullable="false" />
</fragment>
Resht answered 13/4, 2022 at 11:25 Comment(0)
R
0

In your build.gradle app level add this:

apply plugin: 'androidx.navigation.safeargs'

Update:

This codelab is very old try not updating libraries.

Ripleigh answered 2/3, 2019 at 10:27 Comment(5)
apply plugin: 'androidx.navigation.safeargs' Already there in app level gradle.Branchiopod
What version of android studio you're using.Ripleigh
Android studio 3.3.1Branchiopod
or delete whole build folder.Ripleigh
Let us continue this discussion in chat.Ripleigh
U
0

Try to add

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

Instead of

classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-rc02"
Unfit answered 2/3, 2019 at 10:43 Comment(1)
Could not find androidx.navigation:safe-args-gradle-plugin:1.0.0-rc02.Branchiopod
R
0

Version 3.3.1 of Android Studio have similar problems like this but solution is simple. Close project and Import your project again. You will see the result.

Resonant answered 2/3, 2019 at 10:53 Comment(0)
S
0

I tried everything which is written above i.e adding all plugins and dependencies (which is completely right though) but I was doing this silly mistake -> I was creating navArgs variable outside the fragment

Don't ->

private val args by navArgs<VideoCallFragmentArgs>()

class YourFragment : Fragment() {

     override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
          ): View? {
    return inflater.inflate(R.layout.fragment_video_call, container, false)
}
}

Always create navArgs variable inside the fragment.
Do ->

class YourFragment : Fragment() {

private val args by navArgs<VideoCallFragmentArgs>()

     override fun onCreateView(
      inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?
          ): View? {
    return inflater.inflate(R.layout.fragment_video_call, container, false)
}
}
Skeptic answered 25/9, 2021 at 18:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.