我正在尝试在 Android 中使用 imageview 插入多个 textview 。这就是为什么我有以下代码:
public void onItemClick (AdapterView<?> 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);
}
但是当我在 MultiAutoCompleteTextView 中输入任何文本时,它会出现异常。logcat 显示以下内容:
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)
如果我添加此行,则代码有效: sb.append(contactname + ","); 为什么 ?我该如何解决这个错误?为什么会出现这个错误?