当前位置: 首页 > news >正文

java.lang.IndexOutOfBoundsException: setSpan ( 0...x ) ends beyond length X

1,可能是EditText,setSelection(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);
}

另:

拼写检查的设置位置:

http://www.lryc.cn/news/430675.html

相关文章:

  • 技术进展:CH-90树脂在去除硫酸钠柠檬酸钠溶液中铁锰离子上的应用
  • 录屏时摄像头无法识别?如何录屏时打开摄像头,解决方案及录屏软件推荐
  • 达梦数据库-DM8 企业版安装指南
  • 心脑血管科董田林医生:心律失常患者饮食,调养秘诀,助你找回健康心跳
  • 期权杂记(一)
  • 【MATLAB源码-第163期】基于matlab的BPSK+瑞利(rayleigh)信道下有无波束成形误码率对比仿真。
  • 【数据分享】2000-2022年我国省市县三级的逐日O3数据(免费获取\excel\shp格式)
  • Python 的http.server库详细介绍
  • 使用ffmpeg在视频中绘制矩形区域
  • 计算机,数学,AI在社会模拟中的应用
  • 【数据结构】排序算法系列——插入排序(附源码+图解)
  • TOMATO靶机漏洞复现
  • 高基数 GroupBy 在 SLS SQL 中的查询加速
  • TP5队列和TP5 使用redis 等相关
  • 【R语言速通】1.数据类型
  • 【C++设计模式】(三)创建型模式:单例模式
  • 基于Android Studio的行程记录APK开发指南(三)---界面设计及两种方法获取用户位置
  • 大厂趋势:低代码不等于低能力,赋能高效开发新纪元
  • CentOS全面停服,国产化提速,央国企信创即时通讯/协同门户如何选型?
  • 如何确定Kubernetes是在采用哪种方式进行部署的?
  • 【PostgreSQL】地理空间数据的数据类型定义、索引优化、查询优化策略
  • RocketMQ广播消费消息
  • C#基础(2)枚举
  • Linux之MySQL日志
  • Redis集群模式—主从集群、哨兵集群、分片集群
  • 并发工具类(二):CyclicBarrier
  • Spring Cloud全解析:负载均衡之Ribbon简介
  • Kettle安装与使用指南
  • 教育行业解决方案:智能PPT在教育行业的创新应用
  • Matlab程序练习