MAUI: Pending exception java.lang.IndexOutOfBoundsException: setSpan (15 ... 15) ends beyond length 12
I am getting below exception for an Entry
's TextChangedEventArgs
event.
Pending exception java.lang.IndexOutOfBoundsException: setSpan (15 ... 15) ends beyond length 12 (Throwable with no stack trace) JNI DETECTED ERROR IN APPLICATION: JNI CallObjectMethodA called with pending exception java.lang.IndexOutOfBoundsException: setSpan (15 ... 15) ends beyond length 12 (Throwable with no stack trace) in call to CallObjectMethodA from void crc6477f0d89a9cfd64b1.EntryRendererBase_1.n_onTextChanged(java.lang.CharSequence, int, int, int)
Phone number is inputting on the Entry and using TextChangedEventArgs
I am formating it based on the count code. For eg: if the phone number starts with +1, I will format the text like +1 123 123 1234. the phone number starts with +91, I will format the text like +1 12345 12345. If no code, I will show like 123 123 1234.
I am facing the above exception when copy paste the phone number starts with +91. Below is the code:
public async void CallCustomersList(object sender, TextChangedEventArgs args)
{
try
{
int currentLength = args.NewTextValue.Length;
if (currentLength == 0)
{
//do some actions
}
else
{
var text = SearchEntry.Text;
if(IsDigitsOnly(text.Replace(" ","")) || text.Contains("+"))
{
if (text.Length != 0)
{
text = RemoveSpecialCharacter(text, SearchEntry);
string prefix = SearchEntry.Text.Substring(0, 2);
string indPrefix = SearchEntry.Text.Substring(0, 3);
if (text.StartsWith("+1"))
{
if (text.Length > 8)
{
isUsPhoneNumberFormatStarted = true;
SearchEntry.Text = text.Substring(0, 2) + " " + text.Substring(2, 3) + " " + text.Substring(5, 3) + " " + text.Substring(8);
}
else if (text.Length > 5)
{
isUsPhoneNumberFormatStarted = true;
SearchEntry.Text = text.Substring(0, 2) + " " + text.Substring(2, 3) + " " + text.Substring(5);
}
else if (text.Length > 2)
{
isUsPhoneNumberFormatStarted = true;
SearchEntry.Text = text.Substring(0, 2) + " " + text.Substring(2);
}
else
{
SearchEntry.Text = text;
}
SearchEntry.MaxLength = 15;
}
else if(text.StartsWith("+91"))
{
Debug.WriteLine("text:>>" + text);
Debug.WriteLine("length:>>" + text.Length);
isUsPhoneNumberFormatStarted = true;
if (text.Length > 8)
{
SearchEntry.Text = text.Substring(0, 3) + " " + text.Substring(3, 5) + " " + text.Substring(8);
}
else if (text.Length > 3)
{
SearchEntry.Text = text.Substring(0, 3) + " " + text.Substring(3);
}
else
{
SearchEntry.Text = text;
}
SearchEntry.MaxLength = 15;
}
else
{
if (text.Length > 6)
{
isUsPhoneNumberFormatStarted = true;
SearchEntry.Text = text.Substring(0, 3) + " " + text.Substring(3, 3) + " " + text.Substring(6);
}
else if (text.Length > 3)
{
isUsPhoneNumberFormatStarted = true;
SearchEntry.Text = text.Substring(0, 3) + " " + text.Substring(3);
}
else
{
SearchEntry.Text = text;
}
SearchEntry.MaxLength = 12;
}
SearchEntry.CursorPosition = SearchEntry.Text.Length;
}
}
else
{
SearchEntry.MaxLength = 100;
}
if (IsDigitsOnly(text.Replace(" ", "")) || text.Contains("+"))
{
if (args.NewTextValue.Contains("+1"))
{
Preferences.Default.Set("keyword", args.NewTextValue.Replace(" ", "").Remove(0,2));
}
else
{
Preferences.Default.Set("keyword", args.NewTextValue.Replace(" ", ""));
}
}
else
{
if (isUsPhoneNumberFormatStarted)
{
SearchEntry.Text = args.NewTextValue.Replace(" ", "");
Preferences.Default.Set("keyword", SearchEntry.Text);
isUsPhoneNumberFormatStarted = false;
}
else
{
Preferences.Default.Set("keyword", args.NewTextValue);
}
}
}
Debug.WriteLine("keyword:>>" + Preferences.Default.Get("keyword", ""));
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Exception123:>" + e);
}
}
public string RemoveSpecialCharacter(string phoneNumber, Entry SelectedEntry)
{
string[] chars = new string[] { ",", " ", ".", "/", "!", "@", "#", "$", "%", "^", "&", "*", "'", "\"", ";", "_", "(", ")", ":", "|", "[", "]", "-" };
for (int i = 0; i < chars.Length; i++)
{
if (phoneNumber.Contains(chars[i]))
{
phoneNumber = phoneNumber.Replace(chars[i], "");
}
}
return phoneNumber;
}
bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
I used the same codes and try to recreate this issue on a demo project, but on that it is working fine. If we type or paste the phone number it is working as expected. But on the working project, I am facing the above exception.
By checking the code and exception can anyone suggest any suggestions?