【问题标题】:IndexOutOfBoundsException when clicking on FAB with EditText on Oreo在 Oreo 上单击带有 EditText 的 FAB 时出现 IndexOutOfBoundsException
【发布时间】:2018-05-28 14:39:15
【问题描述】:

当用户单击 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$7.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()); 这一行抛出了错误,这意味着atat+1 对于正确的索引来说太大或太小。我认为当用户选择字段的最后一个字符时,at+1 太大了,然后at+1 超出了字段并给你一个例外。在调用replace()之前,您需要添加一个if条件来检查at+1是否在范围内

标签: java android indexoutofboundsexception


【解决方案1】:

如果你的选择在最后,这行肯定会崩溃

field.getText().replace(at, at + 1, view.getText());

如果编辑文本中有 6 个字符,则不能替换 6-7 之间的值。它总是会通过 IndexOutOfBoundsException

像你在下一行做的检查,它不会崩溃

if (field.getSelectionStart() <= FIELD_LENGTH) {

【讨论】:

  • 谢谢!但不得不稍微改变你的解决方案。必须用 检查 if 条件
  • 哦,是的,对不起,我忘记了:p
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多