简体   繁体   English

为什么 args 类不为导航图片段生成?

[英]Why does args classes not generating for navigation graph fragments?

I am using navigation-components in my android project.I have enabled Gradle's type safe args plugin here is my build.gradle(app) file.我在我的 android 项目中使用导航组件。我启用了 Gradle 的类型安全 args 插件,这里是我的build.gradle(app)文件。

apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"
apply plugin: 'com.google.gms.google-services'


android {
   compileSdkVersion 29
   buildToolsVersion "29.0.2"

defaultConfig {
    applicationId "com.example.nibotransporti"
    minSdkVersion 21
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 }
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
dataBinding.enabled = true
compileOptions {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
};

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.legacy:legacy-support-core-utils:1.0.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata:2.2.0"
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-analytics:17.2.3'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-firestore:21.4.1'
implementation 'com.google.android.material:material:1.1.0'
implementation 'de.hdodenhof:circleimageview:3.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
implementation 'com.robertlevonyan.view:CustomFloatingActionButton:3.0.0'
implementation 'com.shuhart.stepview:stepview:1.5.1'
implementation 'com.alespero:expandable-cardview:0.8'
implementation 'com.anton46:stepsview:0.0.2'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.guava:guava:27.0.1-android'
implementation 'com.kofigyan.stateprogressbar:stateprogressbar:1.0.0'
implementation 'androidx.navigation:navigation-fragment:2.2.1'
implementation 'androidx.navigation:navigation-ui:2.2.1'
implementation "androidx.fragment:fragment:1.3.0-alpha01"

}

and my build.gradle(project) file.和我的build.gradle(project)文件。

buildscript {
repositories {
    google()
    jcenter()
    maven { url "https://jitpack.io" }

}
dependencies {
    classpath 'com.android.tools.build:gradle:3.6.1'
    classpath 'com.google.gms:google-services:4.3.3'
    def nav_version = "2.3.0-alpha03"
    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()
    maven { url "https://jitpack.io" }

  }
 }

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

My navigation graph xml code is here as.我的导航图xml 代码在这里。

<?xml version="1.0" encoding="utf-8"?> <br/>
<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/signInFragment">

<fragment
    android:id="@+id/signInFragment"
    android:name="com.example.nibotransporti.Fragment.SignInFragment"
    android:label="fragment_sign_in"
    tools:layout="@layout/fragment_sign_in" >
    <action
        android:id="@+id/action_registration"
        app:destination="@id/signUpFragment">
        <argument
            android:name="email"
            android:defaultValue="3"
            app:argType="string" />
    </action>
    <action
        android:id="@+id/action_signInFragment_to_workerAnalyticsFragment"
        app:destination="@id/workerAnalyticsFragment" />
</fragment>

<fragment
    android:id="@+id/signUpFragment"
    android:name="com.example.nibotransporti.Fragment.SignUpFragment"
    android:label="fragment_sign_up"
    tools:layout="@layout/fragment_sign_up">

    <action
        android:id="@+id/action_signUpFragment_to_signInFragment"
        app:destination="@id/signInFragment">
        <argument
            android:name="email"
            android:defaultValue="ten"
            app:argType="string" />
        <argument
            android:name="password"
            android:defaultValue="wen"
            app:argType="string" />

    </action>
</fragment>


<fragment
    android:id="@+id/workerAnalyticsFragment"
    android:name="com.example.nibotransporti.Fragment.WorkerAnalyticsFragment"
    android:label="WorkerAnalyticsFragment"
    tools:layout="@layout/fragment_woker_analytics"/>
<fragment
    android:id="@+id/reportFragment"
    android:name="com.example.nibotransporti.Fragment.ReportFragment"
    android:label="fragment_stock"
    tools:layout="@layout/fragment_stock" />

</navigation>

Even after several attempts of building the project,the args class for the destination fragment are not creating.即使在多次尝试构建项目之后,目标片段的args类也没有创建。
Here is my code of the fragment in which the action is originating (SignUpFragment)这是我发起动作的片段的代码(SignUpFragment)

SignUpFragmentDirections.ActionSignUpFragmentToSignInFragment action =
                            SignUpFragmentDirections.actionSignUpFragmentToSignInFragment();
                    action.setEmail(email);
                    action.setPassword(password);

And here is the destination code of the fragment (SignInFragment) where i want to access those args .这是我想访问这些args的片段(SignInFragment)的目标代码。 email & password .电子邮件密码

SignInFragmentArgs.getArguments().getString()
                                    //Set up the navigation for the appropriate action
                                    Navigation.findNavController(v).navigate(action);

According to the official documentation the SignInFragmentArgs code should be generated during building the project but it is not and i am in a serious trouble because i want to access those passed arguments email and password from SignUpFragment but i cannot find SignInFragmentArgs class which means that it is not generated during building.根据官方文档,应该在构建项目期间生成SignInFragmentArgs代码,但事实并非如此,我遇到了严重的麻烦,因为我想从SignUpFragment访问那些传递的参数电子邮件密码,但我找不到SignInFragmentArgs类,这意味着它是不是在构建过程中生成的。
Anyone here please i need the solution.任何人都请我需要解决方案。

Your <argument> elements need to be on the <fragment> classes, not on the <action> elements - arguments on actions only provide override values for the arguments already on the destination they point to.你的<argument>元素需要在<fragment>类上,而不是在<action>元素上 - <action>上的参数只为它们指向的目标上已有的参数提供覆盖值。

<?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/signInFragment">

    <fragment
      android:id="@+id/signInFragment"
      android:name="com.example.nibotransporti.Fragment.SignInFragment"
      android:label="fragment_sign_in"
      tools:layout="@layout/fragment_sign_in" >
        <argument
          android:name="email"
          android:defaultValue="ten"
          app:argType="string" />
        <argument
          android:name="password"
          android:defaultValue="wen"
          app:argType="string" />
        <action
          android:id="@+id/action_registration"
          app:destination="@id/signUpFragment" />
        <action
          android:id="@+id/action_signInFragment_to_workerAnalyticsFragment"
          app:destination="@id/workerAnalyticsFragment" />
    </fragment>

    <fragment
      android:id="@+id/signUpFragment"
      android:name="com.example.nibotransporti.Fragment.SignUpFragment"
      android:label="fragment_sign_up"
      tools:layout="@layout/fragment_sign_up">
        <argument
          android:name="email"
          android:defaultValue="3"
          app:argType="string" />

        <action
          android:id="@+id/action_signUpFragment_to_signInFragment"
          app:destination="@id/signInFragment" />
    </fragment>

    <fragment
      android:id="@+id/workerAnalyticsFragment"
      android:name="com.example.nibotransporti.Fragment.WorkerAnalyticsFragment"
      android:label="WorkerAnalyticsFragment"
      tools:layout="@layout/fragment_woker_analytics"/>
    <fragment
      android:id="@+id/reportFragment"
      android:name="com.example.nibotransporti.Fragment.ReportFragment"
      android:label="fragment_stock"
      tools:layout="@layout/fragment_stock" />
</navigation>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM