简体   繁体   English

在导航中添加安全参数时出错

[英]Error while adding safeargs in Navigation

I was trying to add safeargs to my fragments.我试图将安全参数添加到我的片段中。 I was following this codelab: Navigation Codelab .我正在关注这个代码实验室: Navigation Codelab However, when I use the following line:但是,当我使用以下行时:

val safeArgs = FlowStepFragmentArgs by navArgs()

I get a compiler error:我得到一个编译器错误:

Classifier 'FlowStepFragmentArgs' does not have a companion object, and thus must be initialized here分类器“FlowStepFragmentArgs”没有伴随的 object,因此必须在此处初始化

Here is my main gradle file:这是我的主要 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
}

and my app gradle file:和我的应用 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"
}

My navigation.xml is:我的 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>

I have cleaned the project multiple times, but to no effect.我已经多次清理项目,但没有效果。 Can anyone please point out the mistake that I am making here?谁能指出我在这里犯的错误?

I think there are mistake in your navigation.xml file:我认为您的navigation.xml文件中有错误:

You have this action in home_dest fragment:您在home_dest片段中有此操作:

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

But id and destination are arguments for action .但是iddestination是 arguments for action Change it by this way and try again:以这种方式更改并重试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 SafeArgs 和 Mockito 测试导航组件 - Test Navigation Component with SafeArgs and Mockito 如何在使用导航组件和 safeArgs 从回收器视图导航到片段时添加共享元素转换? - How to add shared element transition when navigating from recycler view to a fragment while using navigation components and safeArgs? 在 Kotlin 中使用 safeArgs 进行导航抛出和错误 - Navigating with safeArgs in Kotlin throws and error 导航组件:将安全参数从活动传递到片段 - Navigation Component: pass safeArgs from activity to a fragment gradle 中的导航组件 safeArgs 依赖项在哪里写 - where to write the Navigation Component safeArgs dependencies in gradle 在导航抽屉中添加Google Maps Activity时出错(材质设计) - Error while adding Google Maps Activity in navigation drawer (material Design) 向导航抽屉ListView添加复选框时出错 - Error while adding Check boxes to Navigation Drawer ListView Android 导航组件通过 safeArgs 和深度链接传递参数 - Android navigation component passing argument with safeArgs and deep link Android SafeArgs Navigation Unreslved Reference camelcase kotlin vs underscore xml - Android SafeArgs Navigation Unreslved Reference camelcase kotlin vs underscore xml 原因:androidx.navigation.safeargs 只能与 androidx 项目一起使用 - Cause: androidx.navigation.safeargs can only be used with an androidx project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM