gpt4 book ai didi

Android 兼容性 ExceptionInInitializerError

转载 作者:行者123 更新时间:2023-11-30 04:43:14 29 4
gpt4 key购买 nike

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

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)

我做错了什么?

最佳答案

使用反射。请参阅:determine API level for platforms < 1.6

关于Android 兼容性 ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5570919/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com