faranjit / currency_edittext

Simple currency formatter for Android EditText
Apache License 2.0
66 stars 21 forks source link

Crash in formatter with no easy way to catch it #4

Open StainlessStlRat opened 7 years ago

StainlessStlRat commented 7 years ago

java.lang.NumberFormatException: For input string: " 0002" at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1306) at java.lang.Double.parseDouble(Double.java:547) at faranjit.currency.edittext.CurrencyEditText.format(CurrencyEditText.java:192) at faranjit.currency.edittext.CurrencyEditText.access$300(CurrencyEditText.java:22) at faranjit.currency.edittext.CurrencyEditText$1.onTextChanged(CurrencyEditText.java:171) at android.widget.TextView.sendOnTextChanged(TextView.java:8448) at android.widget.TextView.handleTextChanged(TextView.java:8510) at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:10764) at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1208) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:578) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:509) at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:508) at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:845) at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:198) at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:345) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:91) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154)

AmadoMata commented 6 years ago

This happens during the "TextChanged" event, the "text" variable is formatted to eliminate the symbols and spaces. But it fails to delete the space at the beginning of the value, in your case "0002".

I decided to create my own class based on CurrencyEditText and added the line:

text = text.replaceAll ("\ s +", "");

and that works.

I also added that line in the "getCurrencyDouble" function because an exception was also presented.

These are my modifications so they can be added to this library:

        String text = s.toString();
        text = text.replace(groupDivider, '\u0020').replace(monetaryDivider, '\u0020')
                .replace(".", "").replace(" ", "")
                .replace(currencySymbol, "").trim();
        text = text.replaceAll("\\s+", "");
AlexJuca commented 5 years ago

Since this is not being maintained I will fork this and create an independent project.

faranjit commented 5 years ago

Hi, Sorry for my late response. I cant reproduce this crash :(

lesio commented 5 years ago

Hi, This is happening on the Android PIE for me 😢

rgielow commented 5 years ago

Hi, for me happening only on Android Pie. The edit text insert automatically blank space and .trim or replace not remove this.

Runman44 commented 5 years ago

Thanks @AmadoMata - this fixes the issue for me :)