Safeargs library doesnt generate direction class

前端 未结 19 919
孤街浪徒
孤街浪徒 2021-02-01 11:44

I use navigation library and safeargs for passing data. I define argument to fragment like that.



        
相关标签:
19条回答
  • 2021-02-01 12:21

    I forgot to apply plugin in app.gradle file, just add this line

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

    or this line if you are using java

    apply plugin: "androidx.navigation.safeargs"
    
    0 讨论(0)
  • 2021-02-01 12:22

    Took a bit to figure out but you need both action and arguments in your nav xml file to generate both.

    <fragment
        android:id="@+id/nav_someId"
        android:name=".CategoriesFragment"
        android:label="@string/someName"
        tools:layout="@layout/fragment_categories">
        <argument
            android:name="@string/category"
            android:defaultValue="0"
            app:argType="string" />
        <action
            android:id="@+id/startCategoriesFragment"
            app:destination="@+id/categoriesFragment">
            <argument
                android:name="@string/category"
                android:defaultValue="0"
                app:argType="string" />
        </action>
    </fragment>
    
    0 讨论(0)
  • 2021-02-01 12:24

    Following two steps works for me:

    1. Clean project.
    2. Rebuild project

    Android studio 4.0 Kotlin 1.3.72

    0 讨论(0)
  • 2021-02-01 12:24

    Also make sure to use an up to date version. Had the problem, that I had an 1.X version.

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.2.1"
    implementation "androidx.navigation:navigation-ui-ktx:2.2.1"
    

    And the args correctely in the nav.xml:

    <fragment
        android:id="@+id/categoryFragment"
        android:name="com...CategoryFragment"
        android:label="CategoryFragment"
        tools:layout="@layout/fragment_category">
        <action
            android:id="@+id/action_categoryFragment_to_ProductFragment" // /!\
            app:destination="@id/productFragment"
            <argument
                android:name="products"
                android:defaultValue=""
                app:argType="string" />
        </action>
    </fragment>
    

    Code:

    override fun onItemClicked(category: Category) {
         val action = CategoryFragmentDirections.actionCategoryToProductFragment(products = "myString") // /!\
         view?.findNavController()?.navigate(action)
    }
    
    0 讨论(0)
  • 2021-02-01 12:24

    the problem may be with your apply plugin thing please check if you are using java then add:

    apply plugin: "androidx.navigation.safeargs"
    

    and for kotlin use this:

    apply plugin: "androidx.navigation.safeargs.kotlin"
    
    0 讨论(0)
  • 2021-02-01 12:25

    I faced a similar issue working with android studio 4.2 preview.

    The issue was that the IDE somehow was not picking the source folders where the generated direction classes where being placed by the plugin. So adding the following to my app build.gradle file resolved the issue for me.

    sourceSets {
        main {
            java {
                srcDirs += 'build/generated/source/navigation-args'
            }
        }
    }
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题