Glide报错:找不到GeneratedAppGlideModule,RecyclerView图片加载异常
运行应用时出现以下Glide警告,且RecyclerView中图片加载异常,但应用其他区域Glide加载图片正常:
W/Glide: Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
相关代码
行布局代码
<?xml version="1.0" encoding="utf-8"?> <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/_180sdp" android:layout_height="@dimen/_90sdp" xmlns:app="http://schemas.android.com/apk/res-auto" app:cardCornerRadius="@dimen/_10sdp" app:cardUseCompatPadding="true" android:elevation="@dimen/_3sdp" > <ImageView android:id="@+id/img_popular_meal_item" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"></ImageView> </androidx.cardview.widget.CardView>
RecyclerView布局代码
<androidx.recyclerview.widget.RecyclerView android:id="@+id/rec_view_meals_popular" android:layout_width="wrap_content" android:layout_height="@dimen/_90sdp" android:layout_marginStart="@dimen/_25sdp" android:layout_marginEnd="@dimen/_10sdp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_over_popular_items"/>
Adapter类代码
class MostPopularAdapter(): RecyclerView.Adapter<MostPopularAdapter.PopularMealViewHolder>() { private var mealsList = ArrayList<CategoryMeal>() fun setMeal(mealsList:ArrayList<CategoryMeal>) { this.mealsList=mealsList notifyDataSetChanged() } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PopularMealViewHolder { return PopularMealViewHolder(PopularItemsRowLayoutBinding.inflate(LayoutInflater.from(parent.context),parent,false)) } override fun onBindViewHolder(holder: PopularMealViewHolder, position: Int) { Glide.with(holder.itemView) .load(mealsList[position].strMealThumb) .into(holder.binding.imgPopularMealItem) } override fun getItemCount(): Int { return mealsList.size } inner class PopularMealViewHolder( var binding: PopularItemsRowLayoutBinding):RecyclerView.ViewHolder(binding.root) }
HomeFragment代码
class HomeFragment : Fragment() { lateinit var binding: FragmentHomeBinding lateinit var homeViewModel:HomeViewModel lateinit var randomMeal: Meal lateinit var popularItemAdapter: MostPopularAdapter var mealsList = ArrayList<CategoryMeal>() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val repository = Repository() val homeViewModelFactory = HomeViewModelFactory(repository) homeViewModel=ViewModelProvider(this,homeViewModelFactory).get(HomeViewModel::class.java) popularItemAdapter= MostPopularAdapter() } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { binding= FragmentHomeBinding.inflate(inflater,container,false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) preparePopularItemsRecyclerView() observePopularItems() } fun observePopularItems(){ homeViewModel.getPopularItems("Seafood") homeViewModel.result.observe(viewLifecycleOwner, Observer { popularItemAdapter.setMeal(mealsList) }) } // setup RecyclerView fun preparePopularItemsRecyclerView(){ binding.recViewMealsPopular.apply { layoutManager=LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false) adapter=popularItemAdapter } } }
已尝试的无效方案
创建了GlideAppModule类,但尝试用GlideAppModule.with(...)替代Glide.with(...)时,编译器找不到with()方法:
import com.bumptech.glide.annotation.GlideModule import com.bumptech.glide.module.AppGlideModule @GlideModule class GlideAppModule: AppGlideModule()
正确解决方案
1. 添加Glide编译器依赖
在模块级build.gradle中添加与Glide核心库版本一致的注解处理器依赖:
dependencies { implementation 'com.github.bumptech.glide:glide:4.16.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0' }
2. 使用自动生成的GlideApp类
创建GlideAppModule后,执行Rebuild Project,Glide会自动生成GlideApp类(无需手动调用自己写的GlideAppModule)。修改Adapter中的Glide调用:
GlideApp.with(holder.itemView) .load(mealsList[position].strMealThumb) .into(holder.binding.imgPopularMealItem)
3. 修复HomeFragment数据传递问题
observePopularItems方法中未将ViewModel返回的结果赋值给mealsList,导致Adapter拿到空列表,修改代码:
fun observePopularItems(){ homeViewModel.getPopularItems("Seafood") homeViewModel.result.observe(viewLifecycleOwner, Observer { result -> mealsList = result as ArrayList<CategoryMeal> popularItemAdapter.setMeal(mealsList) }) }
4. 清理并重建项目
执行Clean Project -> Rebuild Project,确保注解处理器生成GlideApp类并清除编译缓存。
内容的提问来源于stack exchange,提问作者Beginner developer

