fragmentDirections.action() 方法找不到在 nav_graph 上指定的参数

fragmentDirections.action() method can't find arguments specified on nav_graph

我正在尝试实现 Safe Args 插件,但我自动生成的 class/method MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment() 似乎无法从 nav_graph 中找到指定的参数。我认为问题不在于插件未生成 classes,因为我能够引用必要的 class 和方法 - 我的问题是操作方法无法识别我的论点和给我一个警告:

Too many arguments for public final fun actionMainScreenFragmentToNoteScreenFragment(): NavDirections defined in com.example.notelite.ui.mainscreen.MainScreenFragmentDirections

我已经在 gradle 上应用了所有必要的插件和依赖项。

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
    id 'kotlin-parcelize'
    id 'androidx.navigation.safeargs.kotlin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.notelite"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    //Navigation Components
    def nav_version = "2.3.5"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    // Feature module Support
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"


    // ViewModel
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"

    // LiveData
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"

    //Room Database
    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    //Kotlin Extensions and Coroutines support for Room
    implementation "androidx.room:room-ktx:$room_version"
    //Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    //Dagger Hilt - Dependency Injection
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-compiler:$hilt_version"

    //Preferences DataStore
    implementation "androidx.datastore:datastore-preferences:1.0.0-beta01"

}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.4.30'
    ext.hilt_version = '2.35'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        def nav_version = "2.3.5"
        classpath 'com.android.tools.build:gradle:4.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的 nav_graph 代码:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mainScreenFragment">
    <fragment
        android:id="@+id/mainScreenFragment"
        android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
        android:label="MainScreenFragment"
        tools:layout="@layout/fragment_mainscreen">
        <action
            android:id="@+id/action_mainScreenFragment_to_noteScreenFragment"
            app:destination="@id/noteScreenFragment" />
        <argument android:name="noteid"
            app:argType="integer"
            android:defaultValue="0" />
    </fragment>
    <fragment
        android:id="@+id/noteScreenFragment"
        android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
        android:label="NoteScreenFragment"
        tools:layout="@layout/fragment_note_screen">
        <action
            android:id="@+id/action_noteScreenFragment_to_noteEditScreenFragment"
            app:destination="@id/noteEditScreenFragment" />
    </fragment>
    <fragment
        android:id="@+id/noteEditScreenFragment"
        android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
        android:label="NoteEditScreenFragment"
        tools:layout="@layout/fragment_edit_note_screen"/>
</navigation>

这是我尝试在其中调用方法的代码片段 - 仅定义导航操作并尝试传递参数参数:

override fun onNoteClick(note: NoteEntity) {
        val noteid = note.id
        val action = MainScreenFragmentDirections.actionMainScreenFragmentToNoteScreenFragment(noteid)
    }

“noteid”是 Room 数据库中项目的键值 table。我已经相应地指定了类型。有任何想法吗?找不到有类似问题的人。我已经尝试清理和重建,重新启动 Android Studio,更新一些插件,更新 IDE... 没有任何效果。有趣的是,我似乎无法在 java(generated) 文件夹中找到生成的 classes,即使我能够在我的片段中引用它们——但这可能只是我一个新手,只是真的找不到他们

您的 <argument> 需要与您导航到的 <fragment> 目的地相关联:您的 NoteScreenFragment:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/mainScreenFragment">
    <fragment
        android:id="@+id/mainScreenFragment"
        android:name="com.example.notelite.ui.mainscreen.MainScreenFragment"
        android:label="MainScreenFragment"
        tools:layout="@layout/fragment_mainscreen">
        <action
            android:id="@+id/action_mainScreenFragment_to_noteScreenFragment"
            app:destination="@id/noteScreenFragment" />
    </fragment>
    <fragment
        android:id="@+id/noteScreenFragment"
        android:name="com.example.notelite.ui.notescreen.NoteScreenFragment"
        android:label="NoteScreenFragment"
        tools:layout="@layout/fragment_note_screen">
        <argument android:name="noteid"
            app:argType="integer"
            android:defaultValue="0" />
        <action
            android:id="@+id/action_noteScreenFragment_to_noteEditScreenFragment"
            app:destination="@id/noteEditScreenFragment" />
    </fragment>
    <fragment
        android:id="@+id/noteEditScreenFragment"
        android:name="com.example.notelite.ui.notescreen.NoteEditScreenFragment"
        android:label="NoteEditScreenFragment"
        tools:layout="@layout/fragment_edit_note_screen"/>
</navigation>