indexoutofboundexception : setSpan (2. .. 2) Length exceeded by 1
I have a MultiAutoCompleteTextView
custom control where I create a piece of paper text when the user presses the spacebar.
When the text box is empty, I don’t want the user to enter a space in the first place, so I put an inputFilter
to prevent the user from entering a space in the first place.
The filter code is as follows:
private void RestrictUselessSpaces(){
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (Character.isWhitespace(source.charAt(i))) {
if(!getText().toString().trim().equals(""))
{
return " ";
}
else
{
return "";
}
}
}
return null;
}
};
setFilters(new InputFilter[]{filter});
}
Somehow, when I comment on the RestrictUselessSpaces
function. It works fine. But when this function runs. Compared to me trying to enter spaces after characters. It gives me an indexoutofboundexception
error. This is the code I encountered the error with.
public void setChips(String s){
if(s.contains(" ") && !s.trim().equals("")) // check space in string
{
this.chips = s.trim().split(" ");
SpannableStringBuilder ssb = new SpannableStringBuilder(getText());
split string wich comma
int x =0;
loop will generate ImageSpan for every country name separated by comma
for(String c : chips){
inflate chips_edittext layout
LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(R.layout.chips_edittext, null);
textView.setText(c); set text
setFlags(textView, c); set flag image
capture bitmapt of genreated textview
int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
textView.measure(spec, spec);
textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.translate(-textView.getScrollX(), -textView.getScrollY());
textView.draw(canvas);
textView.setDrawingCacheEnabled(true);
Bitmap cacheBmp = textView.getDrawingCache();
Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
textView.destroyDrawingCache(); destory drawable
create bitmap drawable for imagespan
BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x = x+ c.length() +1;
}
set chips span
setText(ssb);
move cursor to last
setSelection(s.length());
}
}
The ssb.setSpan
line is causing the problem, but when I debug the code. The debugger ran this line without giving me any exceptions. An error occurred after executing this function, which made it harder for me to determine what was really causing the problem.
Here is my log:
08-10 20:04:19.253: E/AndroidRuntime(18461): FATAL EXCEPTION: main
08-10 20:04:19.253: E/AndroidRuntime(18461): Process: com.example.twitter, PID: 18461
08-10 20:04:19.253: E/AndroidRuntime(18461): java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 1
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.Selection.setSelection(Selection.java:76)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.Selection.setSelection(Selection.java:87)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.EditText.setSelection(EditText.java:94)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.example.libraries.ChipsMultiAutoCompleteTextview.setChips(ChipsMultiAutoCompleteTextview.java:281)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.example.libraries.ChipsMultiAutoCompleteTextview$1.onTextChanged(ChipsMultiAutoCompleteTextview.java:192)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView.sendOnTextChanged(TextView.java:7429)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView.handleTextChanged(TextView.java:7488)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9203)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.os.Handler.dispatchMessage(Handler.java:102)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.os.Looper.loop(Looper.java:212)
08-10 20:04:19.253: E/AndroidRuntime(18461): at android.app.ActivityThread.main(ActivityThread.java:5151)
08-10 20:04:19.253: E/AndroidRuntime(18461): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 20:04:19.253: E/AndroidRuntime(18461): at java.lang.reflect.Method.invoke(Method.java:515)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
08-10 20:04:19.253: E/AndroidRuntime(18461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-10 20:04:19.253: E/AndroidRuntime(18461): at dalvik.system.NativeStart.main(Native Method)
Solution
Try using this
setSelection(s.length() - 1);