IndexOutOfBoundsException while setting text in EditText with TextWatcher addTextChangedListener ()
I got a problem when changing text in EditText, expected result is clear old text, then set new text that user enters. After that, every work with editText is normal.
Exp: old text: 'abcdef',
user press key 'k',
expected text in editText is: 'k'
Here is my code:
editText.addTextChangedListener(new TextWatcher() {
private boolean firstRun = true;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(firstRun) {
editText.removeTextChangedListener(this);
firstRun = false;
if(count == 0) {
// Delete key or other special key
editText.setText("");
return;
} else {
if(s.length() > start) {
editText.setText(String.valueOf(s.charAt(start)));
editText.setSelection(etp.getEditText().getText().length());
} else {
editText.setText("");
}
}
}
}
My code crashed if the user inserts new text inside the old text (it doesn't break if the user inserts from the beginning or end of the old text).
Exp: Old text: abcdef
User action: abcd(paste here)ef
Logarithm below
08-14 06:18:55.977: E/AndroidRuntime(4187): FATAL EXCEPTION: main
08-14 06:18:55.977: E/AndroidRuntime(4187): java.lang.IndexOutOfBoundsException: replace (-1 ... -1) starts before 0
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1021)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:441)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.TextView.replaceText_internal(TextView.java:8442)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.TextView.prepareSpacesAroundPaste(TextView.java:8250)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.TextView.paste(TextView.java:8271)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.TextView.onTextContextMenuItem(TextView.java:8036)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.Editor$ActionPopupWindow.onClick(Editor.java:2862)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.view.View.performClick(View.java:4204)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.view.View$PerformClick.run(View.java:17355)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.os.Handler.handleCallback(Handler.java:725)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.os.Looper.loop(Looper.java:137)
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-14 06:18:55.977: E/AndroidRuntime(4187): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 06:18:55.977: E/AndroidRuntime(4187): at java.lang.reflect.Method.invoke(Method.java:511)
08-14 06:18:55.977: E/AndroidRuntime(4187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-14 06:18:55.977: E/AndroidRuntime(4187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-14 06:18:55.977: E/AndroidRuntime(4187): at dalvik.system.NativeStart.main(Native Method)
What is causing this error?
source to share
This is an unintended side effect of the function that adds spaces before and after nested text. The big key here is on the stack trace:
08-14 06:18:55.977: E/AndroidRuntime(4187): at android.widget.TextView.prepareSpacesAroundPaste(TextView.java:8250)
You wrote code in TextWatcher
that sets text to an empty string ( editText.setText("")
), but the code in prepareSpacesAroundPaste
expects the original text. Hence exclusion from the range of exclusions.
Solution: try replacing the text in the event instead afterTextChanged
; you may need to set the flag onTextChanged
specified in afterTextChanged
before updating the text value.
source to share
addTextChangedListener()
only supports one character change at a time. So you get the error. So this listener won't help you insert multiple texts at once.
Solution So it will be difficult instead. Use a hint .
yourTextView.setHint(oldPassword);
So inserting any key won't be a problem. Now you don't need addTextChangedListener()
, but
PS
Use password inputType in XML by following
android:inputType = password
source to share