广州做网站公司,给公司做网站要多少钱,集团有限公司,赣州网上商城系统1#xff0c;可能是EditText#xff0c;setSelection(x)时超过了 输入框内容的实际长度导致的。
2#xff0c;手机开启“拼写检查功能”#xff0c;EditText设置了最大长度#xff0c;选择提示的某一项文案时超过设置的最大长度限制#xff0c;导致崩溃。
针对情况2 开…1可能是EditTextsetSelection(x)时超过了 输入框内容的实际长度导致的。
2手机开启“拼写检查功能”EditText设置了最大长度选择提示的某一项文案时超过设置的最大长度限制导致崩溃。
针对情况2 开启“拼写检查功能” 的举例
例如通过InputFilter 或者maxLength 设置了EditText的最大长度为8。 ↑ 此时已经输入了7位 ↑ 再输入一个空格此时已经达到最大长度8。
但已经输入的字符有红色横线显示可以进行提示点击已输入的字符就会展示下方样式。 此时如果选择“Que tak”是没问题的因为没有达到最大长度限制。 但选择“quest nak”就会崩溃报IndexOutOfBoundsException。。 解决办法给对应的editText 设置 setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)。
-------------------------------------------------------------------------------------------------------------------------------
原因 参考原文java.lang.IndexOutOfBoundsException: setSpan ( ... ) ends beyond length 剖析
看trackstack的信息应该是点击其中一项候选词的时候发生的奔溃。让我们先看看SuggesttionsPopupWindow在处理点击事件的时候做了什么。
Overridepublic void onItemClick(AdapterView? parent, View view, int position, long id) {SuggestionInfo suggestionInfo mSuggestionInfos[position];replaceWithSuggestion(suggestionInfo);hideWithCleanUp();
}代码很简洁我们继续看看replaceWithSuggestion这个方法代码挺长的只保留重点
private void replaceWithSuggestion(NonNull final SuggestionInfo suggestionInfo) {···//suggestion是点击选中的候选词final String suggestion suggestionInfo.mText.subSequence(suggestionStart, suggestionEnd).toString();//替换mTextView.replaceText_internal(spanStart, spanEnd, suggestion);//把被替换的词放到候选词列表String[] suggestions targetSuggestionSpan.getSuggestions();suggestions[suggestionInfo.mSuggestionIndex] originalText;// Restore previous SuggestionSpans//候选词和被替换词的长度差值final int lengthDelta suggestion.length() - (spanEnd - spanStart);for (int i 0; i length; i) {// Only spans that include the modified region make sense after replacement// Spans partially included in the replaced region are removed, there is no// way to assign them a valid range after replacementif (suggestionSpansStarts[i] spanStart suggestionSpansEnds[i] spanEnd) {//这里在计算setSpan_internal的时候end加上了候选词和被替换词的长度差值其实就是默认//候选词替换后可以完全显示假如我们设置了长度限制例如maxlength,就会引发上述的奔溃mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i],suggestionSpansEnds[i] lengthDelta, suggestionSpansFlags[i]);}}// Move cursor at the end of the replaced wordfinal int newCursorPosition spanEnd lengthDelta;//这里也有同样的问题mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition);
}另
拼写检查的设置位置