
在android应用开发中,直接将`r.drawable`生成的整数id存储到外部数据(如json)是不可靠的,因为这些id在不同编译版本或运行时可能发生变化,导致`resources$notfoundexception`。本教程将详细阐述这一问题的原因,并提供一种健壮的解决方案:通过在外部数据中存储drawable资源的字符串名称,然后在运行时使用`context.getresources().getidentifier()`方法动态获取正确的资源id,确保应用稳定加载图像资源。
理解Android资源ID的不稳定性
Android资源系统为应用中的每个资源(如布局、字符串、图片等)分配一个唯一的整数ID,这些ID定义在自动生成的R类中。例如,R.drawable.tomato会对应一个特定的整数值。然而,一个常见的误解是这些整数ID是固定不变的。实际上,这些资源ID是在每次编译时动态生成的,它们并非在不同编译版本之间稳定不变。
当我们将这些整数ID序列化到外部存储(如JSON文件)中时,问题就出现了。如果应用重新编译,或者在不同的设备/Android版本上运行,之前存储的ID可能不再对应正确的资源,甚至可能对应一个不存在的资源。这会导致运行时抛出android.content.res.Resources$NotFoundException异常,提示“Invalid ID”或“Resource ID #0x...”,从而导致应用崩溃。
解决方案:通过字符串名称动态加载Drawable
为了解决资源ID不稳定的问题,最佳实践是在外部数据中存储Drawable资源的字符串名称,而不是其整数ID。例如,对于R.drawable.tomato,我们应该存储字符串"tomato"。然后在应用运行时,利用Android Resources类提供的getIdentifier()方法,根据字符串名称动态查找并获取当前编译环境下的正确资源ID。
1. 更新数据模型
首先,修改你的数据类(例如Ingredient.java),将存储Drawable ID的字段类型从int更改为String。
Ingredient.java 更新示例:
package me.eyrim.foodrecords2;
import com.google.gson.annotations.SerializedName;
public class Ingredient {
@SerializedName("ingredient_name")
private String ingredientName;
// 将 int 更改为 String
@SerializedName("ingredient_drawable_tag")
private String ingredientDrawableTag;
public String getIngredientName() {
return this.ingredientName;
}
// 返回 String 类型
public String getIngredientDrawableTag() {
return this.ingredientDrawableTag;
}
}2. 更新JSON数据结构
相应地,修改你的JSON数据,将ingredient_drawable_tag的值从整数改为对应的Drawable文件名(不包含文件扩展名)。
JSON数据更新示例:
{
"recipe_name": "My test recipe 1 updated",
"recipe_id": "0",
"recipe_desc": "this is a test desc for my test recipe 1",
"ingredients": [{
"ingredient_name": "Tomato",
// 从整数 ID 更改为字符串名称
"ingredient_drawable_tag": "tomato"
},
{
"ingredient_name": "Pepper",
// 从整数 ID 更改为字符串名称
"ingredient_drawable_tag": "pepper"
}
]
}3. 在RecyclerView Adapter中动态加载Drawable
在你的RecyclerView.Adapter中,当绑定数据到视图时(onBindViewHolder方法),使用Context.getResources().getIdentifier()方法来获取Drawable的实际资源ID。
IngredientRecyclerViewAdapter.java 更新示例:
package me.eyrim.foodrecords2.recipeviewactivity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import me.eyrim.foodrecords2.Ingredient; import me.eyrim.foodrecords2.R; // 确保 R 类被导入 public class IngredientRecyclerViewAdapter extends RecyclerView.Adapter{ private final Ingredient[] ingredients; private final Context context; // 存储 Context 实例 // 构造函数需要接收 Context public IngredientRecyclerViewAdapter(Ingredient[] ingredients, Context context) { this.ingredients = ingredients; this.context = context; // 初始化 Context } @NonNull @Override public IngredientViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredient_row, parent, false); return new IngredientViewHolder(view); } @Override public void onBindViewHolder(@NonNull IngredientViewHolder holder, int position) { String drawableName = this.ingredients[position].getIngredientDrawableTag(); // 使用 getIdentifier() 动态获取资源 ID int drawableId = context.getResources().getIdentifier( drawableName, // 资源名称 (字符串) "drawable", // 资源类型 (例如 "drawable", "string", "layout") context.getPackageName() // 应用包名 ); // 检查是否成功找到资源ID,避免找不到资源时崩溃 if (drawableId != 0) { holder.imageView.setImageResource(drawableId); } else { // 如果找不到对应的 drawable,可以设置一个默认的占位符图片 holder.imageView.setImageResource(R.drawable.default_ingredient_placeholder); // 假设你有一个默认图片 // 或者记录日志,以便调试 // Log.w("IngredientAdapter", "Drawable not found for name: " + drawableName); } } @Override public int getItemCount() { return this.ingredients.length; } public static class IngredientViewHolder extends RecyclerView.ViewHolder { // 静态内部类 private final ImageView imageView; // 如果有 TextView,也在这里初始化 // private final TextView textView; public IngredientViewHolder(@NonNull View itemView) { super(itemView); imageView = itemView.findViewById(R.id.ingredient_image); // textView = itemView.findViewById(R.id.ingredient_name_text); // 如果有的话 } } }
关键点说明:
- Context 实例: getIdentifier() 方法需要一个Context实例来访问应用的资源。在RecyclerView.Adapter中,通常通过构造函数传入Context并作为成员变量保存。
-
getIdentifier(name, defType, defPackage) 参数:
- name: 要查找的资源名称(例如,"tomato")。
- defType: 资源的类型,对于图片通常是"drawable"。其他常见类型包括"string"、"layout"、"id"等。
- defPackage: 资源的包名,通常是context.getPackageName(),即当前应用的包名。
- 错误处理: 如果getIdentifier()找不到对应的资源,它会返回0。务必在设置图片前检查这个返回值,以避免Resources$NotFoundException,并可以设置一个默认的占位符图片来提升用户体验。
注意事项与最佳实践
- 命名规范: 确保JSON中的字符串名称与res/drawable文件夹下的实际Drawable文件名(不含扩展名)完全匹配,包括大小写。
- 性能考虑: getIdentifier()方法在运行时通过字符串查找资源,相比直接使用整数ID会有轻微的性能开销。但在大多数RecyclerView场景下,这种开销通常可以忽略不计。如果需要极致性能且资源数量非常庞大,可以考虑在应用启动时将所有字符串名称映射到整数ID的HashMap中,以空间换时间。
- 默认图片: 强烈建议在getIdentifier()返回0时,设置一个默认的占位符图片。这可以防止在资源名称拼写错误或资源缺失时,应用界面出现空白或崩溃。
- 资源管理: 保持Drawable资源文件的命名清晰和一致性,这有助于代码的可读性和维护性。
总结
通过将Drawable资源的整数ID替换为其字符串名称,并在运行时使用Context.getResources().getIdentifier()动态获取ID,我们可以有效地解决Android资源ID不稳定导致的问题。这种方法使得应用能够更健壮地处理外部配置的资源,提高了应用的可维护性和用户体验,是Android开发中处理动态资源加载的推荐实践。










