Android 兼容性 ExceptionInInitializerError

发布于 2024-10-30 23:34:17 字数 1314 浏览 3 评论 0原文

我正在使用兼容性类来构建用户代理字符串:

public abstract class Compatibility {

    private static int sdkInt = 0;

    private static Compatibility instance;

    public static Compatibility getInstance(){
        if (instance!=null) return instance;

        try { sdkInt = Integer.parseInt(Build.VERSION.SDK); } catch (Exception e) {
            sdkInt = 3;
        }
        if (sdkInt<4) {
            instance = new CompatibilityAPI3();
        } else {
            instance = new CompatibilityAPI4();
        }
        return instance;
    }

    public int getSdkInt(){
        return sdkInt;
    }

    public abstract String getUa();

}


public class CompatibilityAPI3 extends Compatibility {
    @Override
    public String getUa() {
        return "unknown|unknown|"+Build.DEVICE+"|"+Build.PRODUCT+"|"+Build.BRAND+"|"+Build.VERSION.SDK;     
    }
}


public class CompatibilityAPI4 extends Compatibility {
    @Override
    public String getUa() {
        return Build.MANUFACTURER+"|"+Build.MODEL+"|"+Build.DEVICE+"|"+Build.PRODUCT+"|"+Build.BRAND+"|"+Build.VERSION.SDK;
    }
}

我看到某些设备上发生此错误:

java.lang.ExceptionInInitializerError
....
Caused by: java.lang.NoSuchFieldError: ?
at ...CompatibilityAPI4.getUa(CompatibilityAPI4.java:8)

我做错了什么?

I'm using a Compatibility class to build a User Agent string:

public abstract class Compatibility {

    private static int sdkInt = 0;

    private static Compatibility instance;

    public static Compatibility getInstance(){
        if (instance!=null) return instance;

        try { sdkInt = Integer.parseInt(Build.VERSION.SDK); } catch (Exception e) {
            sdkInt = 3;
        }
        if (sdkInt<4) {
            instance = new CompatibilityAPI3();
        } else {
            instance = new CompatibilityAPI4();
        }
        return instance;
    }

    public int getSdkInt(){
        return sdkInt;
    }

    public abstract String getUa();

}


public class CompatibilityAPI3 extends Compatibility {
    @Override
    public String getUa() {
        return "unknown|unknown|"+Build.DEVICE+"|"+Build.PRODUCT+"|"+Build.BRAND+"|"+Build.VERSION.SDK;     
    }
}


public class CompatibilityAPI4 extends Compatibility {
    @Override
    public String getUa() {
        return Build.MANUFACTURER+"|"+Build.MODEL+"|"+Build.DEVICE+"|"+Build.PRODUCT+"|"+Build.BRAND+"|"+Build.VERSION.SDK;
    }
}

I see this Error happening on some devices:

java.lang.ExceptionInInitializerError
....
Caused by: java.lang.NoSuchFieldError: ?
at ...CompatibilityAPI4.getUa(CompatibilityAPI4.java:8)

What am I doing wrong?

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

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

发布评论

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

评论(1

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