w446108264 / XhsEmoticonsKeyboard

android emoticonsKeyboard support emoji and user-defined emoticon. easy to integrated into your project
2.85k stars 597 forks source link

en error when adding emoji and text #4

Closed remon closed 8 years ago

remon commented 8 years ago

when adding emoji and text on the same time I found this error

java.lang.IndexOutOfBoundsException: setSpan (9 ... 10) ends beyond length 9

and the problem is in setspan method

if (!isEmoticonMatcher) {
                ImageSpan[] oldSpans = getText().getSpans(start, end, ImageSpan.class);
                if(oldSpans != null){
                    for (int i = 0; i < oldSpans.length; i++) {
                        int startOld = end;
                        int endOld = after + getText().getSpanEnd(oldSpans[i]) - 1;
                        if (startOld >= 0 && endOld > startOld) {
                            ImageSpan imageSpan = new ImageSpan(oldSpans[i].getDrawable(), ImageSpan.ALIGN_BASELINE);
                            getText().removeSpan(oldSpans[i]);
                            getText().setSpan(imageSpan, startOld, endOld, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                        }
                    }
                }
            }