我现在在我的 crashlytics 中看到这个崩溃有一段时间了,但我找不到修复它的方法。它仅发生在装有 Android 6.0.1 的 Nexus 5X 设备上。
崩溃日志:
Fatal Exception: java.lang.IndexOutOfBoundsException: setSpan (4 ... 4) ends beyond length 3
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1090)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:665)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:658)
at android.text.Selection.setSelection(Selection.java:76)
at android.text.Selection.setSelection(Selection.java:87)
at android.widget.EditText.setSelection(EditText.java:98)
at android.widget.EditText.performAccessibilityActionInternal(EditText.java:138)
at android.view.View.performAccessibilityAction(View.java:8889)
at android.view.AccessibilityInteractionController.performAccessibilityActionUiThread(AccessibilityInteractionController.java:668)
at android.view.AccessibilityInteractionController.-wrap6(AccessibilityInteractionController.java)
at android.view.AccessibilityInteractionController$PrivateHandler.handleMessage(AccessibilityInteractionController.java:1194)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5421)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我无法重现崩溃。既不使用物理设备,也不使用模拟器。它似乎与 TextChanged 侦听器有关。当它被执行时它会崩溃。
代码示例:
editTextCotton.addTextChangedListener(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 s) {
calculateRaidLevel();
}
});
public void calculateRaidLevel(){
double CROP_VALUE_COTTON = 1.5;
double cropValue;
int cottonValue = 0;
if(!editTextCotton.getText().toString().isEmpty()) cottonValue = Integer.valueOf(editTextCotton.getText().toString());
cropValue = cottonValue * CROP_VALUE_COTTON;
}