HashMapmDrawables;

发布于 2024-11-09 02:23:52 字数 2389 浏览 12 评论 0原文

我试图在再次加载可绘制对象之前键入可绘制对象 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-11-16 02:23:52

您是否在代码中的其他位置调用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文