title: IndexOutOfBoundsException in SpannableStringBuilder Android
body: <p>I am trying to insert multiple textview with imageview in Android . That is why I have the following code : </p>
  
  <pre><code>public void onItemClick (AdapterView&lt;?&gt; parent, View view, int position, long id) {
           PhoneContactInfo phoneContactInfo = adapter.getItem(position);
           String contactname = phoneContactInfo.contactName;
          // autoCompleteTextView.setText("");
           Toast.makeText(this, " Something is clicked",Toast.LENGTH_LONG).show();
  
           ClickableSpan clickSpan = new ClickableSpan() {
  
                 @Override
                 public void onClick(View view) { 
                     Log.v("clicked", view.getClass().getSimpleName());
  
                   //  int i = ((EditText) view).getSelectionStart();
                   //  int j = ((EditText) view).getSelectionEnd();
                  //   phoneNum.getText().replace(Math.min(i, j ),
                    //         Math.max(i, j ), "", 0, "".length());
  
                 }
  
             };
  
           TextView tv = createContactTextView(contactname);
           View view1 = getLayoutInflater().inflate(R.layout.contact_layout,null);
           TextView tv1 =  (TextView) view1.findViewById(R.id.contactName);
           tv1.setText(contactname);
           view1.setBackgroundResource(R.color.bubble);
           BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(view1);
           bd.setBounds(0, 0, bd.getIntrinsicWidth(),bd.getIntrinsicHeight());
           contactname = contactname.substring(1, contactname.length());
          // sb.append(contactname + ","); 
           sb.setSpan(new ImageSpan(bd), sb.length()-(contactname.length()), sb.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           autoCompleteTextView.setMovementMethod(LinkMovementMethod.getInstance());
           sb.setSpan(clickSpan, sb.length(),
                           sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
           autoCompleteTextView.setText(sb);
  
       }
  
       public TextView createContactTextView(String text){
            TextView tv = new TextView(this);
            tv.setText(text);
            tv.setTextSize(20);
            tv.setBackgroundResource(R.color.bubble);
            tv.setCompoundDrawablesWithIntrinsicBounds(0, 0,android.R.drawable.presence_offline, 0);
            return tv;
      }
  
       public static Object convertViewToDrawable(View view) {
            int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            view.measure(spec, spec);
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
                      Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            c.translate(-view.getScrollX(), -view.getScrollY());
            view.draw(c);
            view.setDrawingCacheEnabled(true);
            Bitmap cacheBmp = view.getDrawingCache();
            Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
            view.destroyDrawingCache();
            return new BitmapDrawable(viewBmp);
  
          }
  </code></pre>
  
  <p>But when I have entered any text in MultiAutoCompleteTextView , it gets exception . THe logcat shows the following : </p>
  
  <pre><code>12-01 14:52:53.498: E/AndroidRuntime(22653): FATAL EXCEPTION: main
  12-01 14:52:53.498: E/AndroidRuntime(22653): Process: com.commlink.messaging, PID: 22653
  12-01 14:52:53.498: E/AndroidRuntime(22653): java.lang.IndexOutOfBoundsException: setSpan (-13 ... 0) starts before 0
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1042)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:603)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:598)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at com.commlink.messaging.New_Message.onItemClick(New_Message.java:84)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:911)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:91)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1201)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AdapterView.performItemClick(AdapterView.java:299)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AbsListView.performItemClick(AbsListView.java:1152)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3014)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.widget.AbsListView$3.run(AbsListView.java:3865)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.os.Handler.handleCallback(Handler.java:808)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.os.Handler.dispatchMessage(Handler.java:103)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.os.Looper.loop(Looper.java:193)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at android.app.ActivityThread.main(ActivityThread.java:5330)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at java.lang.reflect.Method.invokeNative(Native Method)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at java.lang.reflect.Method.invoke(Method.java:515)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
  12-01 14:52:53.498: E/AndroidRuntime(22653):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>The code works if I add this line : sb.append(contactname + ","); Why ? How can I solve this error ? Why does this error occurs ? </p>