Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.IllegalArgumentException due to Spannable TextView

I received this crash report in my Google Play Console which I myself never experience and unable to reproduce.

java.lang.IllegalArgumentException: 
  at android.text.method.WordIterator.checkOffsetIsValid (WordIterator.java:380)
  at android.text.method.WordIterator.isBoundary (WordIterator.java:101)
  at android.widget.Editor$SelectionStartHandleView.positionAtCursorOffset (Editor.java:4300)
  at android.widget.Editor$HandleView.updatePosition (Editor.java:3736)
  at android.widget.Editor$PositionListener.onPreDraw (Editor.java:2513)
  at android.view.ViewTreeObserver.dispatchOnPreDraw (ViewTreeObserver.java:944)
  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:2417)
  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1321)
  at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:6708)
  at android.view.Choreographer$CallbackRecord.run (Choreographer.java:894)
  at android.view.Choreographer.doCallbacks (Choreographer.java:696)
  at android.view.Choreographer.doFrame (Choreographer.java:631)
  at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:880)
  at android.os.Handler.handleCallback (Handler.java:815)
  at android.os.Handler.dispatchMessage (Handler.java:104)
  at android.os.Looper.loop (Looper.java:207)
  at android.app.ActivityThread.main (ActivityThread.java:5728)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:789)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:679)

The crash report didn't mention which class is causing the crash. My best guess is the follow custom TextView.

private void customTextView(TextView view) {
        SpannableStringBuilder spanTxt = new SpannableStringBuilder(
                Html.fromHtml(definition[0]));
        spanTxt.append("\n\nExample");
        spanTxt.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                showAlert();
            }
        }, spanTxt.length() -"Example".length(), spanTxt.length(), 0);
        view.setMovementMethod(LinkMovementMethod.getInstance());
        view.setText(spanTxt, TextView.BufferType.SPANNABLE);
    }

Is there any problem with my custom TextView?

like image 310
user2872856 Avatar asked Mar 11 '18 07:03

user2872856


2 Answers

I have removed your Html.fromHtml(definition[0]) and \n\n. Then it worked well. Please test these two things if these are coming or not.

private void customTextView(TextView view) {
    SpannableStringBuilder spanTxt = new SpannableStringBuilder("Testing");
    spanTxt.append("Example");
    spanTxt.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
          Log.e("tag","show click");
        }
    }, spanTxt.length() -"Example".length(), spanTxt.length(), 0);
    view.setMovementMethod(LinkMovementMethod.getInstance());
    view.setText(spanTxt, TextView.BufferType.SPANNABLE);
}

Cross check your text if it coming for not. This one Html.fromHtml(definition[0]). Not sure why you using \n\n it will goes to next line.

Check these two things. Others are working perfectly.

Thanks.

like image 188
Saveen Avatar answered Sep 30 '22 03:09

Saveen


The exception refers to this line in AOSP. I think this line in your code:

spanTxt.length() -"Example".length()

is creating a negative or otherwise invalid offset value (probably because you're appending "\n\nExample" instead? Is there a way you could just embed that example string in your resource string?

like image 44
Nonos Avatar answered Sep 30 '22 04:09

Nonos