2

I am using code to populate an EditText view with a string from an array. I then use the .setSelection() function to move the cursor to the end of the view to more easily allow the user to append characters to the string. This works well for all strings of 8 characters or less. However, if the string from the array is greater than 8 characters I am receiving the following error message:

java.lang.IndexOutOfBoundsException: setSpan (9 ... 9) ends beyond length 8

Here is the code:

TransactionDataView = (EditText) findViewById(R.id.etTransactionData);
TransactionDataView.setText(TransactionData[TransactionDataIndex]);
TransactionDataView.setSelection(TransactionData[TransactionDataIndex].length());

From the LogCat I can see what the array subscript is, the string value and the length of the string. Immediately after trying to set the selection I receive the IndexOutOfBoundsException.

06-14 10:18:52.244: D/DataEntry(706): TransactionDataIndex = 0
06-14 10:18:52.244: D/DataEntry(706): TransactionData = 123456789
06-14 10:18:52.244: D/DataEntry(706): TransactionData.Length = 9
06-14 10:18:52.244: W/dalvikvm(706): threadid=1: thread exiting with uncaught exception (group=0x42065438)
06-14 10:18:52.264: E/AndroidRuntime(706): FATAL EXCEPTION: main
06-14 10:18:52.264: E/AndroidRuntime(706): java.lang.IllegalStateException: Could not execute method of the activity
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.view.View$1.onClick(View.java:3674)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.view.View.performClick(View.java:4198)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.view.View$PerformClick.run(View.java:17164)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.os.Handler.handleCallback(Handler.java:615)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.os.Looper.loop(Looper.java:137)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.app.ActivityThread.main(ActivityThread.java:4918)
06-14 10:18:52.264: E/AndroidRuntime(706):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 10:18:52.264: E/AndroidRuntime(706):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 10:18:52.264: E/AndroidRuntime(706):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
06-14 10:18:52.264: E/AndroidRuntime(706):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
06-14 10:18:52.264: E/AndroidRuntime(706):  at dalvik.system.NativeStart.main(Native Method)
06-14 10:18:52.264: E/AndroidRuntime(706): Caused by: java.lang.reflect.InvocationTargetException
06-14 10:18:52.264: E/AndroidRuntime(706):  at java.lang.reflect.Method.invokeNative(Native Method)
06-14 10:18:52.264: E/AndroidRuntime(706):  at java.lang.reflect.Method.invoke(Method.java:511)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.view.View$1.onClick(View.java:3669)
06-14 10:18:52.264: E/AndroidRuntime(706):  ... 11 more
06-14 10:18:52.264: E/AndroidRuntime(706): Caused by: java.lang.IndexOutOfBoundsException: setSpan (9 ... 9) ends beyond length 8
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.text.Selection.setSelection(Selection.java:104)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.text.Selection.setSelection(Selection.java:115)
06-14 10:18:52.264: E/AndroidRuntime(706):  at android.widget.EditText.setSelection(EditText.java:108)
06-14 10:18:52.264: E/AndroidRuntime(706):  at com.worldgiftcard.mobileterminal.DataEntry.onClick(DataEntry.java:152)
06-14 10:18:52.264: E/AndroidRuntime(706):  ... 14 more

Has anyone else encountered this? Is this a bug in the Android code? Can anyone suggest a work around?

Any assistance would be appreciated. Thanks.

Howard
  • 19
  • 1
  • 3

2 Answers2

2

Try changing

TransactionDataView.setSelection(TransactionData[TransactionDataIndex].length());

to

TransactionDataView.setSelection(TransactionDataView.getText().length());
Ken Wolf
  • 22,320
  • 6
  • 55
  • 81
  • This is no good for if the string populated into the view is empty the calculated position would be -1 which is illegal and the app crashes. – Howard Jun 14 '13 at 17:14
  • How about wrapping an if statement around it and only setting the selection if it's not empty? Have added the code in an edit. Doesn't really make sense to set a selection on an empty string anyway. – Ken Wolf Jun 14 '13 at 17:15
  • Still no good. This causes the cursor to be place to the left of the last character instead of to the right of it for strings of 1 TO 8 characters, cuts off the 9th character, and the app now crashes when the string is 10 or more characters. Setting the selection on teh length of an empty string sets the cursor to the zero position. To set the cursor to the left of the string you also have to set the selection to the zero position. Makes perfectly good sense to me. – Howard Jun 14 '13 at 17:34
  • What is a TransactionData? If you're trying to set it to the end don't you just want to do `TransactionData.length-1`? – Ken Wolf Jun 14 '13 at 17:37
  • No, as that would (I believe) would return the length of the array, not the length of the string value stored at the TransactionDataIndex position of the array. – Howard Jun 14 '13 at 17:40
  • OK - you haven't mentioned what these things are so it's hard for me to understand what you're trying to do. It's not a problem with EditText though but something in the logic of what you're doing - you're trying to set a selection that is bigger than what is available. Good luck! – Ken Wolf Jun 14 '13 at 17:42
  • Sorry, I thought I had provided enough information: _"I am using code to populate an EditText view with a string from an array."_ _"From the LogCat I can see what the array subscript is, the string value and the length of the string."_ – Howard Jun 14 '13 at 17:57
  • Sorry, I thought I had provided enough information. I previously stated that the data used to populate the EditText was from an array of strings, _"TransactionData"_, indexed by _"TransactionDataIndex"_. I provided the code that populated the TextView from the array as well as the code for the errant .setSelection function used to place the cursor. I don't know what else I could have provided. – Howard Jun 14 '13 at 18:03
  • OK, one last time, try the edit above :) Also, see here (http://stackoverflow.com/questions/6217378/place-cursor-at-the-end-of-text-in-edittext) – Ken Wolf Jun 14 '13 at 18:08
  • I read that post (I remember the comment about the .append function [it didn't work either]). The only difference between what I was doing and what is in the post is that the position for the setSelection is being derived from the EditText view itself; I am using the length of the array variable. They should be the same (they are) but when I changed my code to use the length of the Edit Text view instead of the array variable the app no longer crashes. However, it also does not display more than 8 characters in the EditText view. – Howard Jun 14 '13 at 18:42
  • Interesting... the length of the EditText view is only reporting a maximum of 8 characters, even though I copied a 9 character string into the EditText view. Strange. – Howard Jun 14 '13 at 18:47
  • It appeas as though the "TransactionDataView.setText(TransactionData[TransactionDataIndex]);" is only assigning up to 8 characters of the string to the EditText view. And as it looks as if that is the case, then the .setSelection was not the problem (It did cause the app to crash when a request was made to position the cursor beyond the end of the view [i.e. the IndexOutOfBoundsException] but that was the fault of the previous .setText not placing the entire string in the EditText view. – Howard Jun 14 '13 at 18:52
  • Ok... found the problem. I had set an InputFilter to limit the length of the EditText view to 8 characters. My bad. Thanks for the help. – Howard Jun 17 '13 at 17:25
-1

In your IndexOutOfBoundsException, the length at the end (8) is not the length of the value of the EditText but is its android:maxLength attribute value. In your case, this value is implicitely or explicitely set to 8, hence the reason for your exception. You need to make sure you don't setSelection() beyond the maxLength of your EditText.

Rgds.

Arnaud
  • 1