为什么导航图片段的参数类没有生成?

huangapple go评论74阅读模式
英文:

Why does args classes not generating for navigation graph fragments?

问题

我在我的Android项目中使用了<b> navigation-components </b>。我已经启用了Gradle的类型安全参数插件,以下是我的<b> build.gradle(app) </b>文件。

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"
}

这是我的<b> build.gradle(project) </b>文件。

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"

    
    // 注意:不要将应用程序的依赖项放在这里;它们属于各个模块的build.gradle文件中
   }
 }

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

  }
 }

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

这是我的<b> navigation graph </b>的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"
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>

尽管我尝试了多次构建项目,目标片段的args类仍未创建。这里是发出操作的片段代码**(SignUpFragment)**。

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

这是目标片段的代码**(SignInFragment),我想从中访问那些args**。emailpassword

SignInFragmentArgs.getArguments().getString()
                                   

<details>
<summary>英文:</summary>

I am using &lt;b&gt; navigation-components &lt;/b&gt; in my android project.I have enabled Gradle&#39;s type safe args plugin here is my &lt;b&gt; build.gradle(app) &lt;/b&gt; file.

    apply plugin: &#39;com.android.application&#39;
    apply plugin: &quot;androidx.navigation.safeargs&quot;
    apply plugin: &#39;com.google.gms.google-services&#39;


    android {
       compileSdkVersion 29
       buildToolsVersion &quot;29.0.2&quot;

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

    }

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

    }

and my &lt;b&gt; build.gradle(project) &lt;/b&gt; file.
    
    buildscript {
    repositories {
        google()
        jcenter()
        maven { url &quot;https://jitpack.io&quot; }
        
    }
    dependencies {
        classpath &#39;com.android.tools.build:gradle:3.6.1&#39;
        classpath &#39;com.google.gms:google-services:4.3.3&#39;
        def nav_version = &quot;2.3.0-alpha03&quot;
        classpath &quot;androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version&quot;

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

    allprojects {
      repositories {
        google()
        jcenter()
        maven { url &quot;https://jitpack.io&quot; }

      }
     }

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

My &lt;b&gt; navigation graph &lt;/b&gt; xml code is here as.
    
 &lt;!-- language: lang-xml --&gt;

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;br/&gt;
    &lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:id=&quot;@+id/nav_graph&quot;
    app:startDestination=&quot;@id/signInFragment&quot;&gt;

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

    &lt;fragment
        android:id=&quot;@+id/signUpFragment&quot;
        android:name=&quot;com.example.nibotransporti.Fragment.SignUpFragment&quot;
        android:label=&quot;fragment_sign_up&quot;
        tools:layout=&quot;@layout/fragment_sign_up&quot;&gt;

        &lt;action
            android:id=&quot;@+id/action_signUpFragment_to_signInFragment&quot;
            app:destination=&quot;@id/signInFragment&quot;&gt;
            &lt;argument
                android:name=&quot;email&quot;
                android:defaultValue=&quot;ten&quot;
                app:argType=&quot;string&quot; /&gt;
            &lt;argument
                android:name=&quot;password&quot;
                android:defaultValue=&quot;wen&quot;
                app:argType=&quot;string&quot; /&gt;

        &lt;/action&gt;
    &lt;/fragment&gt;


    &lt;fragment
        android:id=&quot;@+id/workerAnalyticsFragment&quot;
        android:name=&quot;com.example.nibotransporti.Fragment.WorkerAnalyticsFragment&quot;
        android:label=&quot;WorkerAnalyticsFragment&quot;
        tools:layout=&quot;@layout/fragment_woker_analytics&quot;/&gt;
    &lt;fragment
        android:id=&quot;@+id/reportFragment&quot;
        android:name=&quot;com.example.nibotransporti.Fragment.ReportFragment&quot;
        android:label=&quot;fragment_stock&quot;
        tools:layout=&quot;@layout/fragment_stock&quot; /&gt;

    &lt;/navigation&gt;

Even after several attempts of building the project,the **args** class for the destination fragment are not creating.&lt;br/&gt;
Here is my code of the fragment in which the action is originating **(SignUpFragment)** &lt;br/&gt;

    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** . **email** &amp; **password**.&lt;b/&gt;

    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.&lt;br/&gt;
Anyone here please i need the solution.

</details>


# 答案1
**得分**: 9

你的`&lt;argument&gt;`元素需要放在`&lt;fragment&gt;`类上而不是放在`&lt;action&gt;`元素上 - 动作上的参数仅为它们指向的目标上已经存在的参数提供覆盖值

```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"
  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>
英文:

Your &lt;argument&gt; elements need to be on the &lt;fragment&gt; classes, not on the &lt;action&gt; elements - arguments on actions only provide override values for the arguments already on the destination they point to.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:id=&quot;@+id/nav_graph&quot;
app:startDestination=&quot;@id/signInFragment&quot;&gt;
&lt;fragment
android:id=&quot;@+id/signInFragment&quot;
android:name=&quot;com.example.nibotransporti.Fragment.SignInFragment&quot;
android:label=&quot;fragment_sign_in&quot;
tools:layout=&quot;@layout/fragment_sign_in&quot; &gt;
&lt;argument
android:name=&quot;email&quot;
android:defaultValue=&quot;ten&quot;
app:argType=&quot;string&quot; /&gt;
&lt;argument
android:name=&quot;password&quot;
android:defaultValue=&quot;wen&quot;
app:argType=&quot;string&quot; /&gt;
&lt;action
android:id=&quot;@+id/action_registration&quot;
app:destination=&quot;@id/signUpFragment&quot; /&gt;
&lt;action
android:id=&quot;@+id/action_signInFragment_to_workerAnalyticsFragment&quot;
app:destination=&quot;@id/workerAnalyticsFragment&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
android:id=&quot;@+id/signUpFragment&quot;
android:name=&quot;com.example.nibotransporti.Fragment.SignUpFragment&quot;
android:label=&quot;fragment_sign_up&quot;
tools:layout=&quot;@layout/fragment_sign_up&quot;&gt;
&lt;argument
android:name=&quot;email&quot;
android:defaultValue=&quot;3&quot;
app:argType=&quot;string&quot; /&gt;
&lt;action
android:id=&quot;@+id/action_signUpFragment_to_signInFragment&quot;
app:destination=&quot;@id/signInFragment&quot; /&gt;
&lt;/fragment&gt;
&lt;fragment
android:id=&quot;@+id/workerAnalyticsFragment&quot;
android:name=&quot;com.example.nibotransporti.Fragment.WorkerAnalyticsFragment&quot;
android:label=&quot;WorkerAnalyticsFragment&quot;
tools:layout=&quot;@layout/fragment_woker_analytics&quot;/&gt;
&lt;fragment
android:id=&quot;@+id/reportFragment&quot;
android:name=&quot;com.example.nibotransporti.Fragment.ReportFragment&quot;
android:label=&quot;fragment_stock&quot;
tools:layout=&quot;@layout/fragment_stock&quot; /&gt;
&lt;/navigation&gt;

huangapple
  • 本文由 发表于 2020年3月17日 03:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/60712399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定