我对这段代码遇到了很严重的问题,它来自于svg-android:
public class ParserHelper {
private static final Field STRING_CHARS;
static {
try {
STRING_CHARS = String.class.getDeclaredField("value"); //<-- exception here
STRING_CHARS.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private final char[] s;
private final int n;
private char current;
public int pos;
public ParserHelper(String str, int pos) {
try {
this.s = (char[]) STRING_CHARS.get(str);
} catch (Exception e) {
throw new RuntimeException(e);
}
this.pos = pos;
n = s.length;
current = s[pos];
}
STRING_CHARS = String.class.getDeclaredField("value");
抛出异常。
10-09 10:25:58.240: E/AndroidRuntime(3430): Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: No field value in class Ljava/lang/String; (declaration of 'java.lang.String' appears in /system/framework/core-libart.jar)
只在 Android 6.0 Marshmallow 中出现,我无法继续工作。有什么想法吗?
已解决:我没有解决静态初始化问题,但是我改变了char[] s
的初始化:
public class ParserHelper {
// private static final Field STRING_CHARS;
// static {
// try {
// STRING_CHARS = String.class.getDeclaredField("value");
// STRING_CHARS.setAccessible(true);
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
// }
private final char[] s;
private final int n;
private char current;
public int pos;
public ParserHelper(String str, int pos) {
try {
s = new char[str.length()];
str.getChars(0, str.length(), this.s, 0); //<-- here
} catch (Exception e) {
throw new RuntimeException(e);
}
this.pos = pos;
n = s.length;
current = s[pos];
}