1
建立一個用戶代理字符串:Android的兼容性我使用一個兼容性類的ExceptionInInitializerError
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)
我在做什麼錯?