HashMapmDrawables;
我试图在再次加载可绘制对象之前键入可绘制对象 ID,以检查它是否已在内存中可用。我从主题 apk 包中检索主题,并通过 String 检索它们,如 getDrawable 方法中所示。对于我的一生,我不明白为什么我在这条线上得到一个空指针以及为什么它不起作用。
" if (!mDrawables.containsKey(name)) {"
这就是我调用它在我的活动中显示图像的方式。
/* 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;
}
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(auxDrw);
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否在代码中的其他位置调用
ThemeManager
的构造函数?看来您正在尝试实现一个静态单例类,类似的东西吗?我很好奇这里的大局是什么,您是否只是尝试通过字符串文件名而不是资源 ID 来加载资源?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?