gpt4 book ai didi

java - svg-android 库中 getDeclaredField() 上的 Android 6.0 (Marshmallow) 静态初始化异常

转载 作者:搜寻专家 更新时间:2023-10-30 21:06:15 26 4
gpt4 key购买 nike

我在使用这段代码时遇到了一些严重的问题,来自 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];
}

最佳答案

看起来 String 的名为 value 的私有(private)字段(保存字符数组)在 Marshmallow 中被重命名为 ASCII。因此,您在这些行中有一个 RuntimeException(取自 com.larvalabs.svgandroid.ParserHelper 类):

    try {
STRING_CHARS = String.class.getDeclaredField("value");
STRING_CHARS.setAccessible(true);
} catch (Exception e) {
throw new RuntimeException(e);
}

svgandroiddiscontinued ,因此项目作者修复此问题并将新 jar 推送到 maven 的可能性很小。您可以创建自己的 svgandroid 库分支,合并 this pull-request ,构建 jar 并从现在开始使用它而不是 mavenized 版本。

或者你可以走得更远,自己把固定版本推送到mvnrepository。 :)

关于java - svg-android 库中 getDeclaredField() 上的 Android 6.0 (Marshmallow) 静态初始化异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33035866/

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