The following code works just fine with Android 4, but causes an IllegalArgumentException in Android 2.
Any clues?
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)
Below is the validateOffset in Gingerbread code
private void validateOffset(int offset) {
CharacterIterator it = wrapped.getText();
if (offset < it.getBeginIndex() || offset >= it.getEndIndex()) {
throw new IllegalArgumentException();
}
}
and in ICS code is as below
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);
}
}
>= has been changed to > . The end offset checking seems to be wrong in 2.X devices. This is especially true in your case where the offset you are passing to preceding overlaps with the end index of the string. This seems to be bug in framework.
You can find the source in AOSP code at libcore/luni/src/main/java/java/text/RuleBasedBreakIterator.java.
Here's the Gingerbread code and here's the ICS code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With