android - BreakIterator.preceding 在 Android V2 中失败?

标签 android

以下代码适用于 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/

相关文章:

android - 打开带有 Google Play 商店链接的 Android 应用

java - 来自硬编码 X.509 证书的 KeyFactory.generatePublic

Flash 全屏打开时 Android WebView 空白 - ICS 4.0

android - InputConnection.finishComposingText() 方法中的空指针异常

android - 如何在单元测试中接受对话?

android - 如何使 AndroidX 成为新 Android 项目的默认库?

android - Flutter Doctor --android-licenses : Exception in thread "main" java. lang.NoClassDefFoundError

java - 如何获取 FragmentPagerAdapter 中的当前位置?

java - Android Firestore查询: get the number of documents within a collection

java - Android Studio 1.2.2静态导入完成