导航组件 - 未生成方向类

11

我有一个片段A,其中发起到片段B的操作需要接收一个参数。当我尝试在导航前设置参数时,“FragmentADirections”类无法解析。

事实是,我有一个带有参数的片段C前往D,对于这两个片段来说都很好用。

片段A类(“EntrainementAction”):

    package com.example.androidsportsomveille.Fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.Navigation;

import com.example.androidsportsomveille.Classes.Entrainement;
import com.example.androidsportsomveille.R;

public class EntrainementAction extends Fragment {

    private Entrainement entrainement;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_entrainement_action, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        this.entrainement = EntrainementActionArgs.fromBundle(getArguments()).getMyEntrainement();

        // Remplis le layout avec les données de l'enrainement
        populateLayout();

        // Aller à la gestion de l'entrainement
        Button btnGestion = view.findViewById(R.id.btnGestionEntrainement);
        btnGestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Set de l'argument puis navigation
                EntrainementActionDirections.ActionEntrainementActionToEntrainementGestion action =
                        EntrainementActionDirections.actionEntrainementActionToEntrainementGestion(entrainement);
                Navigation.findNavController(view).navigate(action);
            }
        });
    }

    public void populateLayout() {
        TextView txtNom = getView().findViewById(R.id.txtNomEntrainement);
        txtNom.setText(this.entrainement.getNom());
    }
}
Error appear in 'btnGestion.setOnClickListener' where 'EntrainementActionDirections' cannot be resolve. Note that I have other Directions class well generated.
My nav_graph.xml file:
<?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/entrainementListe">

    <fragment
        android:id="@+id/entrainementListe"
        android:name="com.example.androidsportsomveille.Fragments.EntrainementListe"
        android:label="fragment_entrainement_liste"
        tools:layout="@layout/fragment_entrainement_liste" >
        <action
            android:id="@+id/action_entrainementListe_to_entrainementNouveau"
            app:destination="@id/entrainementNouveau" />
        <action
            android:id="@+id/action_entrainementListe_to_entrainementAction"
            app:destination="@id/entrainementAction" />
    </fragment>

    <fragment
        android:id="@+id/entrainementNouveau"
        android:name="com.example.androidsportsomveille.Fragments.EntrainementNouveau"
        android:label="fragment_entrainement_nouveau"
        tools:layout="@layout/fragment_entrainement_nouveau" >
        <action
            android:id="@+id/action_entrainementNouveau_to_entrainementGestion"
            app:destination="@id/entrainementGestion" />
    </fragment>
    <fragment
        android:id="@+id/entrainementAction"
        android:name="com.example.androidsportsomveille.Fragments.EntrainementAction"
        android:label="fragment_entrainement_action"
        tools:layout="@layout/fragment_entrainement_action" >
        <argument
            android:name="myEntrainement"
            app:argType="com.example.androidsportsomveille.Classes.Entrainement" />
        <action
            android:id="@+id/action_entrainementAction_to_entrainementGestion"
            app:destination="@id/entrainementGestion" />
    </fragment>
    <fragment
        android:id="@+id/entrainementGestion"
        android:name="com.example.androidsportsomveille.Fragments.EntrainementGestion"
        android:label="fragment_entrainement_gestion"
        tools:layout="@layout/fragment_entrainement_gestion" >
        <action
            android:id="@+id/action_entrainementGestion_to_entrainementAction"
            app:destination="@id/entrainementAction" />
        <argument
            android:name="myEntrainement"
            app:argType="com.example.androidsportsomveille.Classes.Entrainement" />
    </fragment>
</navigation>

在“entrainementAction”和“entrainementGestion”片段中,没有生成“Directions”类,而在“entrainementListe”和“entrainementNouveau”中已经生成了。 所有片段的xml文件都存在。

3个回答

21

请确保在您的应用程序Gradle文件中(位于顶部)进行了双重检查。

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

只允许在plugins {}块之前使用buildscript {}和其他插件 {}脚本块,不允许使用其他语句。 - behelit

8
午餐休息后,我启动了Android Studio,然后我的Directions类被生成了。我不知道为什么我之前不能使用它们。

4
因为你需要构建应用程序。 - Machhindra Neupane
1
其实,我记得自己多次构建应用程序,这就是为什么我在这里寻求帮助的原因。我相当确定存在缓存冲突,我本应该使其无效,但现在为时已晚 ^^ - Skunka

6

来源:https://developer.android.com/guide/navigation/navigation-getting-started

推荐的导航方式是使用Safe Args Gradle插件。该插件生成简单的对象和构建器类,可在目的地之间实现类型安全的导航和参数传递。

要将Safe Args添加到项目中,请在顶层build.gradle文件中包含以下classpath:

buildscript {
repositories {
    google()
}
dependencies {
    def nav_version = "2.3.5"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
}

为了生成适用于 Java 或混合 Java 和 Kotlin 模块的 Java 代码,请将以下行添加到您的应用程序或模块的 build.gradle 文件中:

apply plugin: "androidx.navigation.safeargs"

根据迁移到AndroidX的要求,您必须在gradle.properties文件中设置android.useAndroidX=true。

启用Safe Args后,插件将生成包含每个已定义动作的类和方法的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接