簡體   English   中英

在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$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());

如果編輯文本中有6個字符,則不能替換6-7之間的值。 它將始終通過IndexOutOfBoundsException

像下一行一樣進行檢查,它不會崩潰

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM