Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringIndexOutOfBoundsException from SpannableStringInternal

I keep recieving the following bug reports from the market:

java.lang.StringIndexOutOfBoundsException: length=51; regionStart=37; regionLength=-15 at
java.lang.String.startEndAndLength(String.java:593) at
java.lang.String.getChars(String.java:902) at
android.text.SpannableStringInternal.getChars(SpannableStringInternal.java:70)
at android.text.TextUtils.getChars(TextUtils.java:70) at
android.text.TextLine.set(TextLine.java:160) at
android.text.Layout.draw(Layout.java:424) at
android.widget.TextView.onDraw(TextView.java:5262) at
android.view.View.draw(View.java:11184) at
android.view.ViewGroup.drawChild(ViewGroup.java:2892) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.widget.ListView.drawChild(ListView.java:3231) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.widget.AbsListView.dispatchDraw(AbsListView.java:2277) at
android.widget.ListView.dispatchDraw(ListView.java:3226) at
android.view.View.draw(View.java:11289) at
android.widget.AbsListView.draw(AbsListView.java:3760) at
android.view.ViewGroup.drawChild(ViewGroup.java:2892) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.ViewGroup.drawChild(ViewGroup.java:2890) at
android.view.ViewGroup.dispatchDraw(ViewGroup.java:2494) at
android.view.View.draw(View.java:11187) at
android.widget.FrameLayout.draw(FrameLayout.java:450) at
com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2291)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2210) at
android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816) at
android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:137) at
android.app.ActivityThread.main(ActivityThread.java:4511) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:511) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747) at
dalvik.system.NativeStart.main(Native Method)

The frustrating part is that this originates from within android itself - and has no starting point at my code. After doing some research this happens only for Hebrew users and i found this link to Google bug - http://code.google.com/p/android/issues/detail?id=30889

I naturally tried to contact the writer and the Google employee assigned to this bug - none of them has the curtsey or replying.

My related code which sets the span is:

if(searchTerm!=null){
    try{
        Spannable str = (Spannable) name.getText();
        int s_ind = fi.getSearchName().indexOf(searchTerm);
        str.setSpan(new ForegroundColorSpan(Color.BLACK),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        str.setSpan(new BackgroundColorSpan(Color.YELLOW),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),s_ind, (s_ind+searchTerm.length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }catch(Exception ex){
        FLogger.logE(DW_CODES.PREDEFINED_ERROR, "Exception while searching for view in predefined "+ex.getMessage());
    }
}

Search term is a part of the name. I want to mark the searchTerm part with different color so it will be more visible to the user.

I invested truly many hours on this and since i didn't even managed to recreate it on my 4 test devices and 8 AVD's I am kind of lost. Any tips \ Suggestions to do the same without Spannables, will be greatly appreciated.

like image 669
Croc Avatar asked Jan 12 '13 21:01

Croc


1 Answers

Hebrew is a right to left language. Android, at least up til 2.3, did not handleright to left well- for example canvas.drawText would draw it backwards. There were unofficial patches that were used by various OEMS on phones going to those areas effected (Isreal, the Middle East, a few other countries), but it isn't universal. Usually errors were seen when a phone without those patches were used in languages that did right to left text. I don't know if Google ever released an official patch, I'm sure they have but there are tens of millions of broken 2.3 phones out there.

Here's the problem- if you try to fix it yourself (a normal way was to reverse the text yourself) it won't work on devices that do have those patches, and those are the majority of phones which will use those languages. So I can't suggest it. You may want to turn off this effect for rtl languages and see if it fixes it- that would be Hebrew, Arabic, and Urdu.

like image 170
Gabe Sechan Avatar answered Oct 13 '22 03:10

Gabe Sechan