1

我试图将安全参数添加到我的片段中。我在关注这个 codelab:Navigation Codelab。但是,当我使用以下行时:

val safeArgs = FlowStepFragmentArgs by navArgs()

我得到一个编译器错误:

分类器“FlowStepFragmentArgs”没有伴随对象,因此必须在此处初始化

这是我的主要 gradle 文件:

buildscript {
    ext {
        version_kotlin = "1.3.41"
        version_core = "1.1.0"
        version_constraint_layout = "1.1.3"
        version_lifecycle_extensions = "2.0.0"
        version_material = "1.0.0"
        version_navigation = "2.1.0"
    }
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$version_kotlin"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$version_navigation"
        // 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
}

和我的应用程序 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'androidx.navigation.safeargs'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId 'com.example.android.navigation'
        minSdkVersion 19
        targetSdkVersion 29
        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"

    implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
    implementation "androidx.core:core:$version_core"
    implementation "com.google.android.material:material:$version_material"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    //Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:$rootProject.version_navigation"
    implementation "androidx.navigation:navigation-ui-ktx:$rootProject.version_navigation"
}

我的 navigation.xml 是:

<?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"
    app:startDestination="@+id/home_dest">
    <fragment
        android:id="@+id/home_dest"
        android:name="com.example.android.codelabs.navigation.HomeFragment"
        android:label="@string/home"
        tools:layout="@layout/home_fragment">

        <action 
             android:id="@+id/next_action"
             app:destination="@+id/flow_step_one_dest">
        </action>
    </fragment>

    <fragment
        android:id="@+id/flow_step_one_dest"
        android:name="com.example.android.codelabs.navigation.FlowStepFragment"
        tools:layout="@layout/flow_step_one_fragment">
        <argument
            android:name="flowStepNumber"
            app:argType="integer"
            android:defaultValue="1"/>

        <action
            android:id="@+id/next_action"
            app:destination="@+id/flow_step_two_dest">
        </action>
    </fragment>

    <fragment
        android:id="@+id/flow_step_two_dest"
            android:name="com.example.android.codelabs.navigation.FlowStepFragment"
        tools:layout="@layout/flow_step_two_fragment">

        <argument
            android:name="flowStepNumber"
            app:argType="integer"
            android:defaultValue="2"/>
        <action
            android:id="@+id/next_action"
            app:popUpTo="@id/home_dest">
        </action>
    </fragment>

    <fragment>
        <android:id="@+id/settings_dest">
        <android:name="com.example.android.codelabs.navigation.SettingsFragment">
        <android:label="@string/settings">
        <tools:layout="@layout/settings_fragment" />
    </fragment>
</navigation>

我已经多次清理项目,但没有效果。谁能指出我在这里犯的错误?

4

1 回答 1

0

navigation.xml我认为您的文件中有错误:

home_dest您在片段中有此操作:

 <action>
     <android:id="@+id/next_action">
     <app:destination="@+id/flow_step_one_dest">
 </action>

但是iddestination是 的论据action。以这种方式更改并重试:

 <action
      android:id="@+id/next_action"
      app:destination="@+id/flow_step_one_dest">
</action>
于 2019-10-31T07:10:14.573 回答