作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在再次加载可绘制对象之前,我试图输入可绘制对象 ID 以检查它是否已在内存中可用。我正在从主题 apk 包中检索主题并通过 String 检索它们,如 getDrawable 方法中所示。对于我的一生,我不明白为什么我会在这条线上得到一个空指针,为什么它不起作用。
" if (!mDrawables.containsKey(name)) {"
这就是我调用它以在我的 Activity 中显示图像的方式。
/* Custom theme */
auxDrw = ThemeManager.getDrawable(ThemeID.FONDO);
// Fondo
if(auxDrw!=null)((LinearLayout)findViewById(R.id.about_layout)).setBackgroundDrawable(auxDrw);
这是主题管理器:
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;
}
最佳答案
您是否在代码的其他地方将构造函数调用到 ThemeManager
?看来您正在尝试实现一个静态单例类,类似的东西?我很好奇这里的大局是什么,您是否只是试图通过字符串文件名而不是资源 ID 加载资源?
关于android - HashMap<String, Drawable> mDrawables;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6066477/
在再次加载可绘制对象之前,我试图输入可绘制对象 ID 以检查它是否已在内存中可用。我正在从主题 apk 包中检索主题并通过 String 检索它们,如 getDrawable 方法中所示。对于我的一生
我是一名优秀的程序员,十分优秀!