3

次のコードは 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);

例外:

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)
4

1 に答える 1

7

以下は、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 コードのソースを見つけることができます。
これがジンジャーブレッドのコードで、これがICS のコードです。

于 2012-08-13T17:19:52.277 に答える