android - 导航组件 - 未生成方向类
问题描述
我有一个片段 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());
}
}
'btnGestion.setOnClickListener' 中出现错误,其中 'EntrainementActionDirections' 无法解决。请注意,我还生成了其他 Directions 类。
我的 nav_graph.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/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”没有生成方向类,而“entrainementListe”和“entrainementNouveau”有。所有片段的 xml 文件都存在。
解决方案
仔细检查以在您的应用程序 Gradle 中包含此内容(在顶部)
plugins {
id "androidx.navigation.safeargs.kotlin"
}
推荐阅读
- angular - 在 Angular 中将数据发送到 firebase 数据库时出现错误
- php - Symfony 表单中的连接属性
- javascript - 如何在 iOS 手机上使用鼠标悬停链接
- c++ - 使用一个 for 循环如何迭代和测试迭代器值?
- javascript - 将原始数据转换为图形就绪数据
- php - Laravel 雄辩选择 1:n 到 1:1
- wordpress - woocommerce重定向到结帐不起作用
- python - Deploying python with docker, images too big
- android - Firebase recyclerview 适配器分页
- java - 如何从使用 Java 反射 API 调用的方法中检索对象?