I am getting error when I change my text selection for bold and italic text

I am trying to bold and unbold, make text italic and unitalic but I get an error when I change the selection of my text especially when my text is bold and italic. My app works fine when I just bold and unbold my text separately but when I bold and Italic my text and I change the selection then I get the following error. I am using the same code for Italic style as for bold given below. Maybe something wrong with getting the spanStart and spanEnd position but I can’t figure it out. Here is my code

lang="java">private void boldText2(){        int typeface = Typeface.NORMAL;        Spannable str = texto.getText();        int selStart = texto.getSelectionStart();        int selEnd = texto.getSelectionEnd();        StyleSpan[] styleSpans = texto.getText().getSpans(selStart, selEnd, StyleSpan.class);             for (StyleSpan styleSpan : styleSpans) {                int oldStyle = styleSpan.getStyle();                typeface = oldStyle;                int spanStart = str.getSpanStart(styleSpan);                int spanEnd = str.getSpanEnd(styleSpan);                 if (oldStyle == Typeface.BOLD_ITALIC) {                    str.setSpan(new StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                    Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                }                if(oldStyle == Typeface.BOLD) {                    str.removeSpan(styleSpan);                    Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                }                 if (spanStart < texto.getSelectionStart()) {                    str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                    Log.d("TAG", "spanStart < texto.getSelectionStart");                    Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                }                 if (spanEnd > texto.getSelectionEnd()) {                    str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                    Log.d("TAG", "spanEnd > texto.getSelectionEnd()");                    Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                }         }         if (typeface != Typeface.BOLD) {     Log.d("TAG", "Bolding section entered");           if (styleSpans.length > 0) {               int lastSpanEnd = 0;               for (StyleSpan styleSpan : styleSpans) {                   int oldStyle = styleSpan.getStyle();                   int spanStart = str.getSpanStart(styleSpan);                   int spanEnd = str.getSpanEnd(styleSpan);                    if (spanStart > lastSpanEnd) {                       str.setSpan(new StyleSpan(Typeface.BOLD), lastSpanEnd, spanStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                       Log.d("TAG", "spanStart > lastSpanEnd and is bolded 1st if condition");                       Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                   }                    lastSpanEnd = spanEnd;                   str.removeSpan(styleSpan);                     if (spanStart < texto.getSelectionStart()) {                       str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                       Log.d("TAG", "spanStart < texto.getSelectionStart()");                       Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                   }                    if (spanEnd > texto.getSelectionEnd()) {                       str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                       Log.d("TAG", "spanEnd > texto.getSelectionEnd()");                       Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + " spanStart: " + spanStart + " spanEnd: " + spanEnd);                   }                    if (oldStyle == Typeface.ITALIC) {                       str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                       Log.d("TAG", "Bold and Italic");                       Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd + "spanStart: " + spanStart + " spanEnd: " + spanEnd);                   }               }                if (selEnd != lastSpanEnd) {                   str.setSpan(new StyleSpan(Typeface.BOLD), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);                   Log.d("TAG", "selEnd != lastSpanEnd");                   Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd);               }           }            else {               str.setSpan(new StyleSpan(Typeface.BOLD), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);               Log.d("TAG", "Last else Bold");               Log.d("TAG", "selStart: " + selStart + " selEnd: " + selEnd);           }       }        } 

here is the error I get

java.lang.IndexOutOfBoundsException: setSpan (-1 … 14) starts before 0