Android Question Crash on clicking selected CSBuilder text in EditText

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Have a simple EditText that has a CSBuilder text in it (for SQL syntax highlighting). When I select a length of text (with the normal standard long click) and then do a click within that text I get a crash with this error message:

java.lang.IndexOutOfBoundsException: getChars (-1 ... 527) starts before 0
at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1314)
at android.text.SpannableStringBuilder.getChars(SpannableStringBuilder.java:1191)
at android.text.TextUtils.getChars(TextUtils.java:98)
at android.text.TextUtils.substring(TextUtils.java:289)
at android.view.inputmethod.BaseInputConnection.getSelectedText(BaseInputConnection.java:532)
at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:286)
at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

The problem is that I can't find where this error originates from. Running in debug mode does cause the same crash but it doesn't point to any B4A line. I tried stepping in debug mode by adding a Click event sub:

B4X:
Sub edtSQL_Click()
    Log("edtSQL_Click")
End Sub

But that doesn't lead me to the origin of the error either.

In case it matters the CSBuilder text does have clickable words, but the error also happens when clicking outside these clickable words. If formatting is turned off, so if the EditText holds plain text
then there is no error.

I am using the latest version of B4A and also the latest Android version.

RBS
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User


It looks this is a know Android bug:

https://stackoverflow.com/questions...dsexception-getchars-7-0-has-end-before-start

There are some solutions mentioned there, but not sure how this should be coded in B4A.

RBS
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
If this is a bug, take a look a date. 2010 year. Do you think that a bug was not corrected ?

Unlike I saw something similar in Google Console. For old OSes (Android 4), automatic test talked something similar.

If troubles happens in case of context menu, it's possible simply to prevent this event.
B4X:
Dim EditText1 As EditText
Dim reflector1 As Reflector
...
reflector1.Target = EditText1
reflector1.SetOnLongClickListener ("EditText1_LongClick")
...
Sub EditText1_LongClick (objectViewTag As Object) As Boolean

    Return True

End Sub
 
Last edited:
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User


This simple code seems to solve my problem:

B4X:
Sub edtSQL_Click()
 If edtSQL.SelectionLength > 0 Then
  edtSQL.SetSelection(edtSQL.Text.Length - 1, edtSQL.Text.Length - 1)
 End If
End Sub


RBS
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
Clear.

Meanwhile I never was able to reproduce an error from Google Console's automatic test.
I attached a test code. It doesn't work like I want. But here is important another - I can't crash.

Strange bug.
 

Attachments

  • Emoji2.zip
    45.3 KB · Views: 291
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User

Thanks to Ilan I have now serialized this troublesome CSBuilder and saved it as a file.
It doesn't contain EnableClickEvents so that needs to be added manually.
Attached my serialized CSBuilder.

RBS
 

Attachments

  • CSBuilder.txt
    285 bytes · Views: 266
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Would be simpler to test if you create a small project that loads it and reproduces the error.

I have put together a demo project and attached the zipped project folder.
Noticed that the crash only happens if we do:

B4X:
cs.EnableClickEvents(edtSQL)

Without that all is fine.

RBS
 

Attachments

  • CrashDemo.zip
    60 KB · Views: 264
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Noticed that the crash only happens if we do:
I tested your project without any change and got no errors. Here are the conditions:
1. OS 6 and OS 7
2. B4A 8.30, not 8.50
3. KVS2 version 2.21, RandomAccess 2.32, SQL 1.50


*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
285
Table : current_med clicked
285
Table : current_med clicked
** Activity (main) Pause, UserClosed = false **
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User


Did you click a selected text?

RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
No, just the click event. I do not think the class module has the long click event supported as is.

As this is a normal EditText shouldn't it allow to select a length of text?
I can do that and then cause the error when clicking in that text.

RBS
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…