2018-12-17 13:23:47
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: setSpan (-1 ... -1) starts before 0
at android.text.SpannableStringInternal.checkRange(SpannableStringInternal.java:442)
at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:163)
at android.text.SpannableStringInternal.setSpan(SpannableStringInternal.java:152)
at android.text.SpannableString.setSpan(SpannableString.java:46)
at android.text.Selection.setSelection(Selection.java:76)
at android.widget.Editor$SelectionModifierCursorController.resetDragAcceleratorState(Editor.java:5790)
at android.widget.Editor$SelectionModifierCursorController.resetTouchOffsets(Editor.java:5780)
at android.widget.Editor.onFocusChanged(Editor.java:1269)
at android.widget.TextView.onFocusChanged(TextView.java:9844)
at android.view.View.clearFocusInternal(View.java:6834)
at android.view.View.unFocus(View.java:6867)
at android.view.ViewGroup.unFocus(ViewGroup.java:1104)
at android.view.ViewGroup.unFocus(ViewGroup.java:1104)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:751)
at android.support.v7.widget.RecyclerView.requestChildFocus(RecyclerView.java:2614)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:757)
at android.view.ViewGroup.requestChildFocus(ViewGroup.java:757)
at android.view.View.handleFocusGainInternal(View.java:6661)
at android.view.View.requestFocusNoSearch(View.java:10935)
at android.view.View.requestFocus(View.java:10914)
at android.view.View.requestFocus(View.java:10881)
at android.view.View.requestFocus(View.java:10823)
at android.view.View.onTouchEvent(View.java:13133)
at android.widget.TextView.onTouchEvent(TextView.java:9930)
at android.view.View.dispatchTouchEvent(View.java:11843)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2981)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2668)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:552)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1959)
at android.app.Activity.dispatchTouchEvent(Activity.java:3526)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:505)
at android.view.View.dispatchPointerEvent(View.java:12082)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5325)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5109)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4623)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4676)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4642)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4769)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4650)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4826)
TextView在 RecyclerView中使用,像对话列表这样的。
当TextView 设置可以滑动选择为true的时候,这个bug可以必先,先选中某些文字,再按住TextView滑动超出TextView边框,这个时候会有选中的文字,但是不会有左右选中的光标出现。再点按其他Textview, 必崩溃,这个是系统的bug,目前没精力去看源码解决这个问题,先记录一下。
解决办法
public class HackyTextView extends TextView {
...
@Override
public boolean dispatchTouchEvent(final MotionEvent event) {
// FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430
int startSelection = getSelectionStart();
int endSelection = getSelectionEnd();
if (startSelection < 0 || endSelection < 0){
Selection.setSelection((Spannable) getText(), getText().length());
} else if (startSelection != endSelection) {
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
final CharSequence text = getText();
setText(null);
setText(text);
}
}
return super.dispatchTouchEvent(event);
}
}