1. This is the stack information. Let's analyze it
Fat exception, thread in main
Process: XXXXXX process id:19914
08-24 19:46:35.785 19914-19914/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.cmcm.shorts, PID: 19914 java.lang.IndexOutOfBoundsException: setSpan (53 ... 58) ends beyond length 28 at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1265) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:684) at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:677) at com.cmcm.cmlive.activity.dialog.VideoForwardDialog.<init>(VideoForwardDialog.java:1186) at com.cmcm.cmlive.activity.adapter.VideoForwardAttentionAdapter$1.handleMessage(VideoForwardAttentionAdapter.java:70) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6776) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
2. The reason for the accident code is that it needs to insert an icon into a string
3. The length of the string is inconsistent
English string, 67 characters in length
You are not friends. Sending the video will cost you image %1$s COS
Chinese string, length 28, that is, 28 characters. I don't know why python says 62 characters. Sorry
You are not friends yet. Sending this video will cost you% 1$s COS
if(Float.parseFloat(mUserChatBean.getData().getCoc()) > 0) { String alt = String.format(getContext().getString(R.string.share_video_dialog_content), mUserChatBean.getData().getCoc()); SpannableStringBuilder altBuilder = new SpannableStringBuilder(alt); Drawable d = getContext().getResources().getDrawable(R.drawable.no_padding_cos_icon); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); altBuilder.setSpan(span, 53, 58, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); mCocText.setText(altBuilder); } else { mCocText.setText(getContext().getString(R.string.share_video_dialog_content_free)); }
4. The length of the string is inconsistent. The main culprit is here. To start from the 53rd character to the 58th character, you need to insert an icon Drawable, but the total length of the Chinese string is only 28 characters. Can you not crash? brother
altBuilder.setSpan(span, 53, 58, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
5. Look at Spannable's constant explanation
seeing the name of a thing one thinks of its function: Spannable.SPAN_EXCLUSIVE_EXCLUSIVE //Not before or after Spannable.SPAN_INCLUSIVE_EXCLUSIVE //Include before not include after Spannable.SPAN_EXCLUSIVE_INCLUSIVE //Before excluding after Spannable.SPAN_INCLUSIVE_INCLUSIVE //Both before and after