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
and in ICS code is as below
>=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 toprecedingoverlaps 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