6

I am receiving error reports in Crashlytics for NPEs in SpannableStringInternal but could not reproduce the issue on any phone or determine the source of it. I'm calling for the Internet to help me identify the underlying issue.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
   at android.text.SpannableStringInternal.hashCode(SpannableStringInternal.java:480)
   at android.text.SpannedString.hashCode(SpannedString.java)
   at java.util.Objects.hashCode(Objects.java:98)
   at android.view.inputmethod.CursorAnchorInfo.<init>(CursorAnchorInfo.java:435)
   at android.view.inputmethod.CursorAnchorInfo.<init>(CursorAnchorInfo.java)
   at android.view.inputmethod.CursorAnchorInfo$Builder.build(CursorAnchorInfo.java:391)
   at android.widget.Editor$CursorAnchorInfoNotifier.updatePosition(Editor.java:4390)
   at android.widget.Editor$PositionListener.onPreDraw(Editor.java:3250)
   at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1013)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2492)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1509)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7051)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
   at android.view.Choreographer.doCallbacks(Choreographer.java:702)
   at android.view.Choreographer.doFrame(Choreographer.java:638)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6692)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

From the source it looks like that somehow the cursor/selection is causing null Spans to be set.

The error only occurs on Android 7: Crashlytics device info

whlk
  • 15,487
  • 13
  • 66
  • 96
  • 4
    I guess we won't be of great help if you do not provide us some example code. – Al0x Dec 01 '17 at 17:56
  • 2
    @Al0x I could post the whole application code here and both you and me would not be any wiser. The application contains over 50 `EditText` with varying behavior. I have no idea when and where the crash is happening, thus could not post any related code. – whlk Dec 02 '17 at 11:56
  • 1
    This is a tough one. I do see in `Build` for `CursorAnchorInfo` that it is not thread safe. (See [here](https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/java/android/view/inputmethod/CursorAnchorInfo.java#L255)). Are you manipulating spans across threads? – Cheticamp Dec 04 '17 at 18:55
  • 2
    Haven't checked this problem occurs but I found a `CursorAnchorInfo` related change in android-n-preview-1 to android-7.1.0_r1 AOSP changelog. The title is b7edebc : Fix inefficient CursorAnchorInfo#hashCode() https://android.googlesource.com/platform/frameworks/base/+/b7edebc%5E%21/ – Toris Dec 05 '17 at 22:53
  • This issue ist still happening for us, mainly Samsung Devices with Android 12. We are not setting spans explicitly ... – Dennis Aug 17 '22 at 13:04

1 Answers1

0

Faced same issue. In my case it was caused by rich text parser lib cwac-richedit witch was setting null spans while parsing html to spannable. Here is fix for it.

Check where do you get CharSequence which is set to EditText, it is very likely that null span is set somewhere there. In my case it wasn't cursor or selection who set those spans.

Chupik
  • 1,040
  • 8
  • 13