为什么从单个实例调用getColor时会发生NullPoInterException?

发布于 2025-02-11 06:44:59 字数 2982 浏览 1 评论 0原文

Google Play崩溃日志:

Caused by: java.lang.NullPointerException: 
  at com.manager.loader.SkinManager.getColor (SkinManager.java:28)
  at base.util.ui.titlebar.BaseTitlebarFragmentActivity.getImmersiveColor (BaseTitlebarFragmentActivity.java:6)
  at base.util.ui.titlebar.BaseTitlebarFragmentActivity.onCreate (BaseTitlebarFragmentActivity.java)

活动:

public abstract class BaseTitlebarFragmentActivity extends FragmentActivity {

    public void onCreate(Bundle savedInstanceState) {
        setImmersive(getImmersiveColor());
        super.onCreate(savedInstanceState);
    }

    protected int getImmersiveColor(){
        return SkinManager.getInstance().getColor(R.color.v8_common_title_bg);
    }

SkinManager:

public class SkinManager implements ISkinLoader {

    private static final Object synchronizedLock = new Object();
    private static SkinManager instance;

    private SkinManager() { }

    public static SkinManager getInstance() {
        if (instance == null) {
            synchronized (synchronizedLock) {
                if (instance == null){
                    instance = new SkinManager();
                }
            }
        }
        return instance;
    }

    public int getColor(int resId){
        int originColor;
        if(mResources == null || isDefaultSkin){
            try {
                originColor = context.getResources().getColor(resId);
                return originColor;
            } catch (Exception e) {
            }
        }

        int trueColor = 0;
        try{
            trueColor = mResources.getColor(resId);
        }catch(Exception e){
            originColor = context.getResources().getColor(resId);
            trueColor = originColor;
        }
        
        return trueColor;
    }

App:

public class App extends BaseApplication {

    public void onCreate() {
        super.onCreate();
        initSkins();
    }

    private void initSkins() {
        AttrFactory.addSupportAttr("iiv_background_color", new IconicsImageViewBgColorAttr());
        AttrFactory.addSupportAttr("iiv_color", new IconicsImageViewColorAttr());
        AttrFactory.addSupportAttr("matProg_barColor", new ProgressWheelBarColorAttr());
        AttrFactory.addSupportAttr("progressDrawable", new ProgressBarDrawableAttr());
        AttrFactory.addSupportAttr("indeterminateDrawable", new ProgressBar2DrawableAttr());
        AttrFactory.addSupportAttr("button", new CheckBoxButtonDrawableAttr());

        SkinManager.getInstance().init(this);
        SkinManager.getInstance().setUpSkinFile();
        if (!ProcessManager.isMainProcess(getContext())) {
            SkinManager.getInstance().registerProcessSkinLoadReceiver();
        }
    }

enter image description here

google play crash log:

Caused by: java.lang.NullPointerException: 
  at com.manager.loader.SkinManager.getColor (SkinManager.java:28)
  at base.util.ui.titlebar.BaseTitlebarFragmentActivity.getImmersiveColor (BaseTitlebarFragmentActivity.java:6)
  at base.util.ui.titlebar.BaseTitlebarFragmentActivity.onCreate (BaseTitlebarFragmentActivity.java)

activity:

public abstract class BaseTitlebarFragmentActivity extends FragmentActivity {

    public void onCreate(Bundle savedInstanceState) {
        setImmersive(getImmersiveColor());
        super.onCreate(savedInstanceState);
    }

    protected int getImmersiveColor(){
        return SkinManager.getInstance().getColor(R.color.v8_common_title_bg);
    }

SkinManager:

public class SkinManager implements ISkinLoader {

    private static final Object synchronizedLock = new Object();
    private static SkinManager instance;

    private SkinManager() { }

    public static SkinManager getInstance() {
        if (instance == null) {
            synchronized (synchronizedLock) {
                if (instance == null){
                    instance = new SkinManager();
                }
            }
        }
        return instance;
    }

    public int getColor(int resId){
        int originColor;
        if(mResources == null || isDefaultSkin){
            try {
                originColor = context.getResources().getColor(resId);
                return originColor;
            } catch (Exception e) {
            }
        }

        int trueColor = 0;
        try{
            trueColor = mResources.getColor(resId);
        }catch(Exception e){
            originColor = context.getResources().getColor(resId);
            trueColor = originColor;
        }
        
        return trueColor;
    }

App:

public class App extends BaseApplication {

    public void onCreate() {
        super.onCreate();
        initSkins();
    }

    private void initSkins() {
        AttrFactory.addSupportAttr("iiv_background_color", new IconicsImageViewBgColorAttr());
        AttrFactory.addSupportAttr("iiv_color", new IconicsImageViewColorAttr());
        AttrFactory.addSupportAttr("matProg_barColor", new ProgressWheelBarColorAttr());
        AttrFactory.addSupportAttr("progressDrawable", new ProgressBarDrawableAttr());
        AttrFactory.addSupportAttr("indeterminateDrawable", new ProgressBar2DrawableAttr());
        AttrFactory.addSupportAttr("button", new CheckBoxButtonDrawableAttr());

        SkinManager.getInstance().init(this);
        SkinManager.getInstance().setUpSkinFile();
        if (!ProcessManager.isMainProcess(getContext())) {
            SkinManager.getInstance().registerProcessSkinLoadReceiver();
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

泪冰清 2025-02-18 06:44:59

尽管正如崩溃日志所示,这是不可能的,但我将重构GetColor,并查看:

    public int getColor(int resId){
        if(mResources == null || isDefaultSkin){
            try {
                int originColor = context.getResources().getColor(resId);
                return originColor;
            } catch (Exception ignored) {
            }
        }

        try{
            int trueColor = mResources.getColor(resId);
            return trueColor;
        } catch(Exception ignored) {
        }

        try {
            int originColor = context.getResources().getColor(resId);
            return originColor;
        } catch (Exception ignored) {
        }

        return 0;
    }

Although it's impossible as crash log shows, I will refactoring getColor to this and have a look:

    public int getColor(int resId){
        if(mResources == null || isDefaultSkin){
            try {
                int originColor = context.getResources().getColor(resId);
                return originColor;
            } catch (Exception ignored) {
            }
        }

        try{
            int trueColor = mResources.getColor(resId);
            return trueColor;
        } catch(Exception ignored) {
        }

        try {
            int originColor = context.getResources().getColor(resId);
            return originColor;
        } catch (Exception ignored) {
        }

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