IndexOutOfBoundsException Editor.java line 2182 only on Samsung Android 10

IndexOutOfBoundsException Editor.java line 2182 only on Samsung Android 10

我遇到了一个崩溃问题,它只出现在带有 Android 10.

的三星设备上
Editor.java line 2182
android.widget.Editor.drawHardwareAcceleratedInne

Fatal Exception: java.lang.IndexOutOfBoundsException: charAt: 10 >= length 4
   at android.text.SpannableStringBuilder.charAt(SpannableStringBuilder.java:125)
   at android.text.TextLine.handleText(TextLine.java:883)
   at android.text.TextLine.handleRun(TextLine.java:1125)
   at android.text.TextLine.drawRun(TextLine.java:491)
   at android.text.TextLine.draw(TextLine.java:286)
   at android.text.Layout.drawText(Layout.java:588)
   at android.widget.Editor.drawHardwareAcceleratedInner(Editor.java:2182)
   at android.widget.Editor.drawHardwareAccelerated(Editor.java:2101)
   at android.widget.Editor.onDraw(Editor.java:2041)
   at android.widget.TextView.onDraw(TextView.java:8750)
   at android.view.View.draw(View.java:23187)
   at android.view.View.updateDisplayListIfDirty(View.java:22062)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.draw(View.java:23190)
   at android.view.View.updateDisplayListIfDirty(View.java:22062)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:4820)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.draw(View.java:23190)
   at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4219)
   at android.view.View.updateDisplayListIfDirty(View.java:22062)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.draw(View.java:23190)
   at androidx.core.widget.NestedScrollView.draw(NestedScrollView.java:1961)
   at android.view.View.updateDisplayListIfDirty(View.java:22062)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.updateDisplayListIfDirty(View.java:22048)
   at android.view.View.draw(View.java:22917)
   at android.view.ViewGroup.drawChild(ViewGroup.java:5230)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4987)
   at android.view.View.draw(View.java:23190)
   at com.android.internal.policy.DecorView.draw(DecorView.java:1154)
   at android.view.View.updateDisplayListIfDirty(View.java:22062)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:588)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:594)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:667)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:4296)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4080)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3348)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2225)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9126)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:999)
   at android.view.Choreographer.doCallbacks(Choreographer.java:797)
   at android.view.Choreographer.doFrame(Choreographer.java:732)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:984)
   at android.os.Handler.handleCallback(Handler.java:883)
   at android.os.Handler.dispatchMessage(Handler.java:100)
   at android.os.Looper.loop(Looper.java:237)
   at android.app.ActivityThread.main(ActivityThread.java:8167)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

原来是因为自定义的textView导致崩溃。

我删除了它并用普通(android SDK)textView 替换了它,现在它工作正常:)

删除的代码如下:

class MyCustomizedTextView : AppCompatTextView {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context, attrs)
    }

    private lateinit var iconTypefaceSpan: CalligraphyTypefaceSpan

    private fun init(context: Context, attrs: AttributeSet) {
        if(!isInEditMode) {
            iconTypefaceSpan = CalligraphyTypefaceSpan(
                TypefaceUtils.load(context.assets,
                               context.getString(R.string.font)
            )
        )
    }
  }
}