FragmentDirections was not generate by Safe Args plugin
Asked Answered
H

3

3

When I trying to use Safe Args plugin to jump from one fragment to another, the compiler shows "Unresolved reference: VideoFragmentDirections". I have already set the classpath and dependency for safe args and VideoFragmentArgs was generated correctly. kotlin_version = '1.3.50' navigation-safe-args-gradle-plugin:2.1.0 In my xml

<fragment
        android:id="@+id/nav_home"
        android:name="example.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home" >
        <action
            android:id="@+id/action_nav_home_to_videoFragment"
            app:destination="@id/videoFragment"
            app:popUpTo="@+id/nav_home"/>
    </fragment>
<fragment
        android:id="@+id/videoFragment"
        android:name="example.ui.videoui.VideoFragment"
        android:label="VideoFragment" >
        <argument
            android:name="id"
            app:argType="long"
            android:defaultValue="0L" />
    </fragment>

And in HomeFragment.ky

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val videoViewModel = ViewModelProviders
            .of(this)
            .get(HomeViewModel::class.java)
        val adapter = video_list.adapter!! as PlaylistAdapter
        videoViewModel.videos.observe(this, Observer<List<Video>> {
            adapter.submitList(it)
            adapter.onItemClickListener = View.OnClickListener { v ->

                val viewHolder = v.tag as RecyclerView.ViewHolder
                val position = viewHolder.adapterPosition
                val id = it[position].id
                val action = VideoFragmentDirections.actionhHomeToVideo(id)
                view.findNavController().navigate(action)
            }
        })
}

Also, I had tried to clean and rebuild the project, it still doesn't work.

Hexapartite answered 25/11, 2019 at 15:24 Comment(0)
P
9

try

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

in app build.gradle

Poach answered 29/11, 2019 at 5:14 Comment(0)
R
8
// Project level build.gradle 
buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version") //add here.
    }
}

// Module level build.gradle.
plugins {
    id 'androidx.navigation.safeargs'// add here.
}

I wish your happy day🙏

Reba answered 22/12, 2021 at 7:38 Comment(2)
This did the trick for me, thanks for your answerFlatboat
Thank you very much !! You save my day. I tried the other method first, and get error messages like Class 'ActionCameraToJpegViewer' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections I am wondering why "androidx.navigation.safeargs.kotlin" cannot solve kotlin's problem but "androidx.navigation.safeargs" can.Genuflect
M
0

Make sure to Rebuild Project after adding the new dependencies. Update the versions in your Project gradle file as well if needed.

This works fine.

Menfolk answered 15/8, 2020 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.