学习RecyclerView时点击条目触发Resources$NotFoundException致应用崩溃求助
解决RecyclerView条目点击启动详情Activity时的Resources$NotFoundException崩溃问题
我为学习Adapter和RecyclerView开发了一款应用,包含RecyclerView与Adapter,计划点击RecyclerView条目时通过数据类展示数据并启动详情Activity,但运行时点击条目会导致应用崩溃,抛出java resources not found exception。
崩溃日志
2022-07-24 16:00:14.903 11460-11460/com.example.planetapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.planetapp, PID: 11460 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.planetapp/com.example.planetapp.PlanetDetail}: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) at android.os.Handler.dispatchMessage(Handler.java:112) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) Caused by: android.content.res.Resources$NotFoundException: Resource ID #0xffffffff at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:292) at android.content.res.Resources.getValue(Resources.java:1540) at androidx.appcompat.widget.ResourceManagerInternal.createDrawableIfNeeded(ResourceManagerInternal.java:180) at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:145) at androidx.appcompat.widget.ResourceManagerInternal.getDrawable(ResourceManagerInternal.java:136) at androidx.appcompat.content.res.AppCompatResources.getDrawable(AppCompatResources.java:66) at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:91) at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:102) at com.example.planetapp.PlanetDetail.setData(PlanetDetail.kt:37) at com.example.planetapp.PlanetDetail.onCreate(PlanetDetail.kt:20) at android.app.Activity.performCreate(Activity.java:7458) at android.app.Activity.performCreate(Activity.java:7448) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) at android.os.Handler.dispatchMessage(Handler.java:112) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
问题分析
从崩溃栈可以明确:崩溃发生在PlanetDetail.kt第37行调用AppCompatImageView.setImageResource()时,传入的资源ID是0xffffffff(即-1),系统找不到对应的资源文件导致崩溃。
解决步骤
- 直接定位
PlanetDetail.kt第37行代码,确认传入setImageResource()的参数值,该值应为合法的drawable资源ID(如R.drawable.planet_mars),而非-1。 - 回溯数据传递链路:
- 检查RecyclerView Adapter中,点击条目时向详情Activity传递的图片资源ID是否正确,是否存在Intent传值错误。
- 检查数据类定义,确保存储图片资源ID的字段类型为
Int,且初始化时赋值的是有效资源ID,而非无效值。
- 在
setData()方法中添加有效值校验,避免传入无效ID:
// PlanetDetail.kt的setData方法内 val imageResId = planet.imageResId if (imageResId != -1) { // 可选:再验证资源是否存在 try { resources.getDrawable(imageResId, theme) planetImageView.setImageResource(imageResId) } catch (e: Resources.NotFoundException) { planetImageView.setImageResource(R.drawable.default_placeholder) } } else { planetImageView.setImageResource(R.drawable.default_placeholder) }
- 调试时可以打印传入的资源ID,快速定位问题:
Log.d("PlanetDetail", "当前图片资源ID: $imageResId")
内容的提问来源于stack exchange,提问作者Catch The Code
相关产品推荐
相关产品推荐

