从自动完成下拉列表中选择项目时IndexOutOfBoundsException

时间:2016-12-04 17:59:46

标签: android-textinputlayout android-spellcheck

我在我的应用中使用split()TextInputLayout以及TextInputEditText,我不允许两个单词之间的空格,所以如果我选择一个空格自动完成的项目下拉或拼写检查,我遇到了android:inputType="textAutoComplete"

IndexOutOfBoundsException

在我的活动中,我使用java.lang.IndexOutOfBoundsException: setSpan (0 ... 8) ends beyond length 7 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677) at android.widget.TextView.setSpan_internal(TextView.java:9900) at android.widget.Editor.replaceWithSuggestion(Editor.java:2654) at android.widget.Editor.-wrap18(Editor.java) at android.widget.Editor$SuggestionsPopupWindow.onItemClick(Editor.java:3638) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1156) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121) at android.widget.AbsListView$3.run(AbsListView.java:4036) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

TextWatcher

我理解private final TextWatcher mTextWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { } @Override public void afterTextChanged(Editable str) { //don't allow special characters if (str.length() > 0) { StringBuilder validatedHandleBuilder = new StringBuilder(); for (char c : str.toCharArray()) { if (mProfileManager.isAlphaNumeric(c)) { validatedHandleBuilder.append(c); } } String validatedHandle = validatedHandleBuilder.toString(); if (!str.equals(validatedHandle)) { int cursorPosition = mTextInputEditText.getSelectionEnd() - 1; mTextInputEditText.setText(validatedHandle); int newCursorPosition = cursorPosition < mTextInputEditText.length() ? cursorPosition : mTextInputEditText.length(); mTextInputEditText.setSelection(newCursorPosition); } } } }; ,但我无法确定我可以处理此异常的代码行。

0 个答案:

没有答案