Android导航组件中的studio构建错误,action不是抽象的,没有实现抽象成员actionID

Android studio build error in navigation component, action is not abstract and does not implement abstract member actionID

不知何故,构建崩溃并出现与导航组件相关的奇怪错误,即使它以前可以工作,错误在生成的 class 中,在我的例子中是 NativeLanguageSelectionFragmentDirections

这里是错误

e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (10, 16): Class 'GoToSelectLearningLangAction' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (13, 12): 'getActionId' overrides nothing
e: C:\Users\David\StudioProjects\android\app\build\generated\source\navigation-args\debug\com\linguistic\linguistic\framework\presentation\loginscreens\ui\main\NativeLanguageSelectionFragmentDirections.kt: (15, 12): 'getArguments' overrides nothing

这里是导航图的代码

<?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/first_login_graph.xml"
    app:startDestination="@id/nativeLanguageSelectionFragment">

    <fragment
        android:id="@+id/nativeLanguageSelectionFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.NativeLanguageSelectionFragment"
        android:label="NativeLanguageSelectionFragment"
        tools:layout="@layout/language_selection_fragment">
        <action
            android:id="@+id/goToSelectLearningLangAction"
            app:destination="@id/learningLanguageSelectionFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/nativeLanguageSelectionFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/learningLanguageSelectionFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.LearningLanguageSelectionFragment"
        android:label="LearningLanguageSelectionFragment"
        tools:layout="@layout/language_selection_fragment">
        <argument
            android:name="nativeLanguageID"
            app:argType="string"
            app:nullable="true" />
        <action
            android:id="@+id/action_learningLanguageSelectionFragment_to_welcomeFragment"
            app:destination="@id/welcomeFragment"
            app:enterAnim="@anim/slide_in_right"
            app:exitAnim="@anim/slide_out_left"
            app:popEnterAnim="@anim/slide_in_left"
            app:popExitAnim="@anim/slide_out_right"
            app:popUpTo="@id/learningLanguageSelectionFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@+id/welcomeFragment"
        android:name="com.linguistic.linguistic.framework.presentation.loginscreens.ui.main.WelcomeFragment"
        android:label="fragment_welcome"
        tools:layout="@layout/fragment_welcome" />
</navigation>

我正在使用

 "androidx.navigation:navigation-fragment-ktx:2.3.5"
 "androidx.navigation:navigation-ui-ktx:2.3.5"

我也有这个问题。 直到他们发布修复程序。 请试试这个:

plugins {
  id("androidx.navigation.safeargs")
}

而不是

plugins {
  id("androidx.navigation.safeargs.kotlin")
}

自版本 v2.4.0-alpha02 发布以来,此已知错误已不存在。

Bug Fixes

  • Safe Args no longer crashes when attempting to generate direction properties in Kotlin. (Id2416, b/188564435)

至于我的情况,我还必须更新 Safe Arg 插件以消除错误消息。

[build.gradle]
buildscript {
    ...
    dependencies {
        ...
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-alpha04'
}

[app/build.gradle]
dependencies {
    ...
    implementation 'androidx.fragment:fragment-ktx:1.4.0-alpha04'
    api 'androidx.navigation:navigation-fragment-ktx:2.4.0-alpha04'
    api 'androidx.navigation:navigation-ui-ktx:2.4.0-alpha04'
}

所有这些依赖项

androidx.navigation:navigation-fragment-ktx
androidx.navigation:navigation-ui-ktx
androidx.navigation:navigation-safe-args-gradle-plugin

需要使用相同的版本 - 例如:在撰写本文时它是 2.4.0-alpha10

2.4.0-beta02 目前可用

只需将 safe-args 依赖更新到最新版本(>=2.4.0-rc01) 即可!:

classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.4.0-rc01"

就我而言,我的新 module 遇到了这个错误。我在 build.gradle (project)

中添加了 safe-args classPath

KOTLIN-DSL:

    "classpath"(group = "androidx.navigation",name = "navigation-safe-args-gradle-plugin", version = "2.4.1")

没有在我的模块中插入 build.gradle。所以我在 build.gradle:

之上添加了这个
id("androidx.navigation.safeargs.kotlin")

GROOVY:

classpath  "navigation-safe-args-gradle-plugin : 2.4.1"

和:

id 'androidx.navigation.safeargs.kotlin'