HashMap<String, Drawable> mDrawables;
I'm trying to to key on the drawable ID to check if it is already available in memory before loading drawables again. I'm retrieving the theme from the theme apk package and retrieving them by String as seen in the getDrawable method. For the life of me I cant see why I get a nullpointer on this line and why it wont work.
" if (!mDrawables.containsKey(name)) {"
This is how I'm calling it to display images in my Activities.
/* Custom theme */
auxDrw = ThemeManager.getDrawable(ThemeID.FONDO);
// Fondo
if(auxDrw!=null)((LinearLayout)findViewById(R.id.about_layout)).setBackgroundDrawable(auxD开发者_Python百科rw);
This is the 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;
}
Are you calling the constructor to ThemeManager
elsewhere in your code? It appears that you are attempting to implement a static singleton class, something along those lines? I'm curious what the bigger picture here is, are you just attempting to load resources by a string filename instead of a resource ID?
精彩评论