Note that there are some explanatory texts on larger screens.

plurals
  1. POEditText setSelection Not Working for Positions Greater Than 8
    text
    copied!<p>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:</p> <pre><code>java.lang.IndexOutOfBoundsException: setSpan (9 ... 9) ends beyond length 8 </code></pre> <p>Here is the code:</p> <pre><code>TransactionDataView = (EditText) findViewById(R.id.etTransactionData); TransactionDataView.setText(TransactionData[TransactionDataIndex]); TransactionDataView.setSelection(TransactionData[TransactionDataIndex].length()); </code></pre> <p>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.</p> <pre><code>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 </code></pre> <p>Has anyone else encountered this? Is this a bug in the Android code? Can anyone suggest a work around?</p> <p>Any assistance would be appreciated. Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload