以下代码适用于 Android 4,但在 Android 2 中会导致 IllegalArgumentException。
有什么线索吗?
Locale currentLocale = new Locale("en_UK");
final BreakIterator boundary = BreakIterator.getSentenceInstance(currentLocale);
boundary.setText("a");
int thisThrowsExceptionInVersion2 = boundary.preceding(1);
异常(exception):
08-08 22:29:14.414: E/AndroidRuntime(329): Caused by: java.lang.IllegalArgumentException
08-08 22:29:14.414: E/AndroidRuntime(329): at java.text.RuleBasedBreakIterator.validateOffset(RuleBasedBreakIterator.java:74)
08-08 22:29:14.414: E/AndroidRuntime(329): at java.text.RuleBasedBreakIterator.preceding(RuleBasedBreakIterator.java:158)
08-08 22:29:14.414: E/AndroidRuntime(329): at kalle.palle.namespace.KallePalleActivity.onCreate(KallePalleActivity.java:26)
最佳答案
下面是 Gingerbread 代码中的validateOffset
private void validateOffset(int offset) {
CharacterIterator it = wrapped.getText();
if (offset < it.getBeginIndex() || offset >= it.getEndIndex()) {
throw new IllegalArgumentException();
}
}
ICS代码如下
private void validateOffset(int offset) {
CharacterIterator it = wrapped.getText();
if (offset < it.getBeginIndex() || offset > it.getEndIndex()) {
String message = "Valid range is [" + it.getBeginIndex() + " " + it.getEndIndex() + "]";
throw new IllegalArgumentException(message);
}
}
>=
已更改为 >
。结束偏移检查在 2.X 设备中似乎是错误的。在您传递给 preceding
的偏移量与字符串的结束索引重叠的情况下尤其如此。这似乎是框架中的错误。
您可以在 libcore/luni/src/main/java/java/text/RuleBasedBreakIterator.java 找到 AOSP 代码中的源代码。
这是 Gingerbread code这是ICS code
关于android - BreakIterator.preceding 在 Android V2 中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11872758/