我正在尝试键入可绘制的ID,以便在再次加载drawable之前检查它是否已在内存中可用。我正在从主题apk包中检索主题并通过String检索它们,如getDrawable方法中所示。对于我的生活,我无法理解为什么我会在这一行上得到一个nullpointer以及为什么它不会工作。
" if (!mDrawables.containsKey(name)) {"
这就是我称之为在我的活动中显示图像的方式。
/* Custom theme */
auxDrw = ThemeManager.getDrawable(ThemeID.FONDO);
// Fondo
if(auxDrw!=null)((LinearLayout)findViewById(R.id.about_layout)).setBackgroundDrawable(auxDrw);
这是ThemeManager:
public static Resources themeResources = null;
public static String themePackage = null;
public static void setTheme(Context ctx, String themePack){
PackageManager pm = ctx.getPackageManager();
themePackage = themePack;
themeResources = null;
try{
themeResources = pm.getResourcesForApplication(themePackage);
}catch(NameNotFoundException e){
Log.d("tag", "Theme not found: "+e.getMessage());
}
}
public static void setTheme(Context ctx){
String themePackageName = Utils.preferencias.getString("themePackageName", "");
ThemeManager.setTheme(ctx, themePackageName);
}
private static boolean mActive = true;
private static HashMap<String, Drawable> mDrawables;
private Context ctx;
public ThemeManager(Context c) {
mDrawables = new HashMap<String, Drawable>();
ctx = c;
}
public static Drawable getDrawable(String name) {
if (mActive) {
if (!mDrawables.containsKey(name)) {
try {
int resource_id = themeResources.getIdentifier(name,
"drawable", themePackage);
if (resource_id != 0) {
mDrawables.put(name,
themeResources.getDrawable(resource_id));
}
} catch (NullPointerException e) {
}
return mDrawables.get(name);
}
}
return null;
}
答案 0 :(得分:1)
您是否在代码中的其他位置调用ThemeManager
的构造函数?您似乎正在尝试实现静态单例类,这些是什么?我很好奇这里的大局是什么,你只是试图通过字符串文件名而不是资源ID来加载资源吗?