Android中Unresolved reference: FragmentDirections问题求助
解决Unresolved reference: FragmentDirections编译错误
问题背景
编译项目时出现Unresolved reference: FragmentDirections错误,已配置androidx.navigation.safeargs.kotlin插件(项目级和模块级均配置),执行过同步、清理、重建操作,问题仍存在;尝试切换到androidx.navigation.safeargs插件时同步报错。
相关代码文件
ListAdapter.kt
import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.navigation.findNavController import androidx.recyclerview.widget.RecyclerView class ComercioListAdapter(private val comercios: List<Comercio>) : RecyclerView.Adapter<ComercioListAdapter.ViewHolder>() { class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val nombreTextView: TextView = view.findViewById(R.id.textNombre) val descripcionTextView: TextView = view.findViewById(R.id.textDireccion) val telefonoTextView: TextView = view.findViewById(R.id.textTelefono) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.item_comercio, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val comercio = comercios[position] holder.nombreTextView.text = comercio.nombre holder.descripcionTextView.text = comercio.direccion holder.telefonoTextView.text = comercio.telefono holder.itemView.setOnClickListener { // 点击商品项跳转 val action = CategoriesFragmentDirections.actionCategoriesFragmentToComercio(comercio) holder.itemView.findNavController().navigate(action) } } override fun getItemCount() = comercios.size }
模块级build.gradle
plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' id 'androidx.navigation.safeargs.kotlin' }
项目级build.gradle
buildscript { repositories { google() } dependencies { def nav_version = "2.5.3" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" } } plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false id 'org.jetbrains.kotlin.android' version '1.8.0' apply false id 'androidx.navigation.safeargs.kotlin' version '2.5.3' apply false }
mobile_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" android:id="@+id/mobile_navigation" app:startDestination="@+id/nav_home"> <fragment android:id="@+id/nav_home" android:name="com.developmentsls.valenciaappguide.English.ui.home.HomeFragment" android:label="@string/menu_home" tools:layout="@layout/fragment_home" /> <fragment android:id="@+id/nav_categories" android:name="com.developmentsls.valenciaappguide.English.ui.categories.CategoriesFragment" android:label="@string/menu_categories" tools:layout="@layout/fragment_categories" /> <fragment android:id="@+id/nav_contact" android:name="com.developmentsls.valenciaappguide.English.ui.contact.ContactFragment" android:label="@string/menu_contact" tools:layout="@layout/fragment_contact" /> </navigation>
问题根源分析
- 导航图缺失关键配置:当前
mobile_navigation.xml中未定义ComercioFragment,也没有从CategoriesFragment到ComercioFragment的跳转action(即actionCategoriesFragmentToComercio),Safe Args插件无法自动生成对应的CategoriesFragmentDirections类。 - 项目级配置冗余:项目级
build.gradle同时在buildscript.dependencies和plugins块声明了Safe Args插件,虽不直接报错,但可能导致插件加载异常。
修复步骤
1. 完善导航图配置
在mobile_navigation.xml中添加ComercioFragment定义及跳转action,并配置参数传递(需确保Comercio类实现Parcelable或Serializable接口):
<?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/mobile_navigation" app:startDestination="@+id/nav_home"> <!-- 原有Fragment保持不变 --> <fragment android:id="@+id/nav_home" android:name="com.developmentsls.valenciaappguide.English.ui.home.HomeFragment" android:label="@string/menu_home" tools:layout="@layout/fragment_home" /> <fragment android:id="@+id/nav_categories" android:name="com.developmentsls.valenciaappguide.English.ui.categories.CategoriesFragment" android:label="@string/menu_categories" tools:layout="@layout/fragment_categories"> <!-- 添加跳转至ComercioFragment的action --> <action android:id="@+id/action_categoriesFragment_to_comercio" app:destination="@+id/comercioFragment" /> </fragment> <fragment android:id="@+id/nav_contact" android:name="com.developmentsls.valenciaappguide.English.ui.contact.ContactFragment" android:label="@string/menu_contact" tools:layout="@layout/fragment_contact" /> <!-- 添加ComercioFragment定义 --> <fragment android:id="@+id/comercioFragment" android:name="com.developmentsls.valenciaappguide.English.ui.comercio.ComercioFragment" android:label="Comercio" tools:layout="@layout/fragment_comercio"> <!-- 配置接收的参数,name为参数名,type为对应类型(需匹配Comercio的序列化类型) --> <argument android:name="comercio" app:argType="com.developmentsls.valenciaappguide.data.Comercio" app:nullable="false" /> </fragment> </navigation>
2. 修正项目级build.gradle配置
移除冗余的插件声明,保留一种配置方式(推荐使用plugins块):
plugins { id 'com.android.application' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false id 'org.jetbrains.kotlin.android' version '1.8.0' apply false id 'androidx.navigation.safeargs.kotlin' version '2.5.3' apply false } // 移除buildscript块的Safe Args配置,避免重复 // buildscript { // repositories { // google() // } // dependencies { // def nav_version = "2.5.3" // classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" // } // }
3. 确保Comercio类实现序列化接口
修改Comercio类,实现Parcelable(推荐)或Serializable:
import android.os.Parcelable import kotlinx.parcelize.Parcelize @Parcelize data class Comercio( val nombre: String, val direccion: String, val telefono: String // 其他字段 ) : Parcelable
注:需在模块级build.gradle中添加Parcelize插件:
plugins { // 原有插件 id 'kotlin-parcelize' }
4. 重新同步、清理并重建项目
执行以下操作:
- 点击Android Studio的
Sync Project with Gradle Files按钮 - 执行
Build > Clean Project - 执行
Build > Rebuild Project
验证
完成上述步骤后,CategoriesFragmentDirections类将由Safe Args自动生成,编译错误应消失。
内容的提问来源于stack exchange,提问作者TheX_H
相关产品推荐
相关产品推荐

