I see this crash for a while now in my crashlytics and I cannot find a way to fix it. It only occurs on Nexus 5X devices with Android 6.0.1.
Crashlog:
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)
I cannot reproduce the crash. Neither with a physical device nor with the emulator. It seems to have something to do with the TextChanged listener. It crashes when this gets executed.
Code Example:
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;
}