I am working in android. i have a problem. my application crashes when i click on my text box second time.
this is my Logcat message:
java.lang.Index
you could add below code after initiation your text box (edit text or etc)
editText.setSaveFromParentEnabled(false);
editText.setSaveEnabled(true);
and for more documentation about this you can read below documentation:
the cause of your problem is the following error:
java.lang.IndexOutOfBoundsException: setSpan (4 ... 4) ends beyond length 0
Apparently you're setting a span on something, but the textfield is empty, giving an IndexOutOfBoundsException
, check the length of the input string before you make a call to setSpan.
EDIT:
Just a short clarification, an IndexOutOfBoundsException
always means you're trying to access part of an array that is beyond the actual length of the array. String objects are defined as arrays of characters. As such when you're trying to do something but the length of the string is equal to zero, you actually end up beyond the boundary of an array.
setSpan (4 ... 4) ends beyond length 0, this means you have a 4 white space character string its actual/trimmed length is zero, setting selection to this will cause IndexOutOfBoundsException, you should check the trimmed length before setting selection
You haven't explained about your code. So it's difficult to get through your problem.
Check this blog. It may help you. This blog says about a time picker issue. The same applies for EditText & TextView also.
setSaveFromParentEnabled(false) & setSaveEnabled(true) should solve the issue.
tp = (TimePicker) findViewById(R.id.timePickerComponent);
//two lines to add after
tp.setSaveFromParentEnabled(false);
tp.setSaveEnabled(true);