Android: there is an error when I click on my editText

I work in android. I have a problem. my application crashes when I click on my text box a second time.

this is my logcat post:

java.lang.IndexOutOfBoundsException: setSpan (4 ... 4) ends beyond length 0 android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:943) android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:522) android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:514) android.text.Selection.setSelection(Selection.java:74) android.text.Selection.setSelection(Selection.java:85) android.text.method.ArrowKeyMovementMethod.onTouchEvent(ArrowKeyMovementMethod.java:410) android.widget.TextView.onTouchEvent(TextView.java:6715) android.widget.EditText.onTouchEvent(EditText.java:190) android.view.View.dispatchTouchEvent(View.java:3766) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936) com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1731) com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1120) android.app.Activity.dispatchTouchEvent(Activity.java:2086) com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1715) android.view.ViewRoot.handleMessage(ViewRoot.java:1787) android.os.Handler.dispatchMessage(Handler.java:99) 

please help me find out the cause of this problem. Thank you in advance.

+7
source share
4 answers

The cause of your problem is the following error: java.lang.IndexOutOfBoundsException: setSpan (4 ... 4) ends beyond length 0

Apparently you are setting the spacing to something, but the text field is empty, giving an IndexOutOfBoundsException , check the length of the input string before making a call to setSpan.

EDIT:

Just a brief explanation, IndexOutOfBoundsException always means that you are trying to access a part of the array that is outside the actual length of the array. String objects are defined as arrays of characters. Thus, when you try to do something, but the length of the string is zero, you actually find yourself outside the bounds of the array.

+2
source

You did not explain your code. Therefore, it is difficult to solve your problem.

Check this blog. This can help you. This blog post talks about timing issues. The same goes for EditText and TextView.

setSaveFromParentEnabled (false) and setSaveEnabled (true) should solve the problem.

 tp = (TimePicker) findViewById(R.id.timePickerComponent); //two lines to add after tp.setSaveFromParentEnabled(false); tp.setSaveEnabled(true); 
+2
source

setSpan (4 ... 4) ends beyond the length of 0, this means that you have 4 lines of space, the actual / cropped length is zero, setting this parameter will throw an IndexOutOfBoundsException, you should check the cropped length before setting the choice

+1
source

you can add the code below after starting your text field (edit the text, etc.)

 editText.setSaveFromParentEnabled(false); editText.setSaveEnabled(true); 

and for more documentation about this, you can read the documentation below:

0
source

All Articles