在 Oreo 上使用 EditText 单击 FAB 时出现 IndexOutOfBoundsException
IndexOutOfBoundsException when clicking on FAB with EditText on Oreo
当用户单击 FAB 以添加计时器或单击 TimerEditActivity 上的数字按钮时抛出 "java.lang.IndexOutOfBoundsException" 错误。这只发生在 Android 8.0 上。我尝试了不同的方法,但不知道如何解决这个问题。
void onClick(TextView view) {
if (mFocusGrabber.isFocused())
return;
EditText field = getFocusedField();
int at = field.getSelectionStart();
field.getText().replace(at, at + 1, view.getText()); //LINE 116, causing the error
field.setSelection(at + 1);
// updateStartButtonVisibility();
if (field.getSelectionStart() == FIELD_LENGTH) {
// At the end of the current field, so try to focus to the next field.
// The search will return null if no view can be focused next.
View next = field.focusSearch(View.FOCUS_RIGHT);
if (next != null) {
next.requestFocus();
if (next instanceof EditText) {
// Should always start off at the beginning of the field
((EditText) next).setSelection(0);
}
}
}
}
这是来自 Play 管理中心的堆栈跟踪。第116行似乎是罪魁祸首。
at android.text.SpannableStringBuilder.checkRange (SpannableStringBuilder.java:1309)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:510)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:504)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:502)
at be.demillennial.oneclock.timers.EditTimerActivity.onClick (EditTimerActivity.java:116)
at be.demillennial.oneclock.timers.EditTimerActivity$$ViewBinder.doClick (EditTimerActivity$$ViewBinder.java:92)
at butterknife.internal.DebouncingOnClickListener.onClick (DebouncingOnClickListener.java:22)
at android.view.View.performClick (View.java:6891)
at android.widget.TextView.performClick (TextView.java:12651)
at android.view.View$PerformClick.run (View.java:26083)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6938)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
如果您的选择在末尾,这一行肯定会崩溃
field.getText().replace(at, at + 1, view.getText());
如果编辑文本中有 6 个字符,则不能替换 6-7 之间的值。它总是会通过 IndexOutOfBoundsException
像你一样在下一行中检查它不会崩溃
if (field.getSelectionStart() <= FIELD_LENGTH) {
当用户单击 FAB 以添加计时器或单击 TimerEditActivity 上的数字按钮时抛出 "java.lang.IndexOutOfBoundsException" 错误。这只发生在 Android 8.0 上。我尝试了不同的方法,但不知道如何解决这个问题。
void onClick(TextView view) {
if (mFocusGrabber.isFocused())
return;
EditText field = getFocusedField();
int at = field.getSelectionStart();
field.getText().replace(at, at + 1, view.getText()); //LINE 116, causing the error
field.setSelection(at + 1);
// updateStartButtonVisibility();
if (field.getSelectionStart() == FIELD_LENGTH) {
// At the end of the current field, so try to focus to the next field.
// The search will return null if no view can be focused next.
View next = field.focusSearch(View.FOCUS_RIGHT);
if (next != null) {
next.requestFocus();
if (next instanceof EditText) {
// Should always start off at the beginning of the field
((EditText) next).setSelection(0);
}
}
}
}
这是来自 Play 管理中心的堆栈跟踪。第116行似乎是罪魁祸首。
at android.text.SpannableStringBuilder.checkRange (SpannableStringBuilder.java:1309)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:510)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:504)
at android.text.SpannableStringBuilder.replace (SpannableStringBuilder.java:502)
at be.demillennial.oneclock.timers.EditTimerActivity.onClick (EditTimerActivity.java:116)
at be.demillennial.oneclock.timers.EditTimerActivity$$ViewBinder.doClick (EditTimerActivity$$ViewBinder.java:92)
at butterknife.internal.DebouncingOnClickListener.onClick (DebouncingOnClickListener.java:22)
at android.view.View.performClick (View.java:6891)
at android.widget.TextView.performClick (TextView.java:12651)
at android.view.View$PerformClick.run (View.java:26083)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:164)
at android.app.ActivityThread.main (ActivityThread.java:6938)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
如果您的选择在末尾,这一行肯定会崩溃
field.getText().replace(at, at + 1, view.getText());
如果编辑文本中有 6 个字符,则不能替换 6-7 之间的值。它总是会通过 IndexOutOfBoundsException
像你一样在下一行中检查它不会崩溃
if (field.getSelectionStart() <= FIELD_LENGTH) {