我遇到以下错误,我不知道我的代码有什么问题。当其粗体和斜体然后斜体文本有时被删除,有时没有,然后出现此错误。
2018-11-10 19:34:03.032 16997-16997 / com.pilum.apps.primalnotes E / Android运行时:致命异常:主要 流程:com.pilum.apps.primalnotes,PID:16997 java.lang.IndexOutOfBoundsException: setSpan(-1 ... -1)在0之前开始 在android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1314) 在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:680) 在android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:672) 在com.apps.primalnotes.Fragments.EditorFragment.italicText2(EditorFragment.java:393) at com.apps.primalnotes.Fragments.EditorFragment.access $ 300(EditorFragment.java:48) 在com.apps.primalnotes.Fragments.EditorFragment $ 5.onClick(EditorFragment.java:129) 在android.view.View.performClick(View.java:6877) 在android.view.View $ PerformClick.run(View.java:26069) 在android.os.Handler.handleCallback(Handler.java:789) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6938) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
private void italicText2(){
boolean italicExists = false;
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();
int spanStart = str.getSpanStart(styleSpan);
int spanEnd = str.getSpanEnd(styleSpan);
if (spanStart < texto.getSelectionStart()) {
str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (spanEnd > texto.getSelectionEnd()) {
str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (selEnd > selStart) {
if (oldStyle == Typeface.ITALIC) {
str.removeSpan(styleSpan);
italicExists = true;
}
if (oldStyle == Typeface.BOLD_ITALIC) {
str.setSpan(new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
italicExists = false;
}
}
}
if (!italicExists) {
Log.d("Tag", "italic 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.ITALIC), lastSpanEnd, spanStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "if italic");
}
lastSpanEnd = spanEnd;
str.removeSpan(styleSpan);
if (oldStyle == Typeface.BOLD) {
str.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "Bold and Italic");
}
else {
**str.setSpan(new StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);**
Log.d("TAG", "else italic");
}
if (spanStart < texto.getSelectionStart()) {
str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "oldstyle italic");
}
if (spanEnd > texto.getSelectionEnd()) {
str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Log.d("TAG", "old style end italic");
}
}
if (selEnd != lastSpanEnd) {
str.setSpan(new StyleSpan(Typeface.ITALIC), selStart, selEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}