Android ADT 21.0 warning: Implicitly using the default locale
AndroidAdtAndroid Problem Overview
I've updated ADT to v. 21 and new warning appeared in this code:
if (e.getMessage().toLowerCase().contains("blabla"))
Implicitly using the default locale is a common source of bugs: Use toLowerCase(Locale) instead
So I try:
if (e.getMessage().toLowerCase(Locale.ENGLISH).contains("blabla"))
But error still remained! How fix this?
Android Solutions
Solution 1 - Android
You should use Locale.getDefault()
especially if you cant be sure that your text will always be in english.
Also lint errors like that one you are having usually disappear after you run lint again or clean your project.
Solution 2 - Android
You simply need to clean your project by clicking:
Build > Clean Project
or Build > Rebuild Project
Solution 3 - Android
Actually, use Locale.getDefault()
when the goal is to present text to the user. However, and this is the whole point of the Lint check, you should probably be using Locale.US
whenever the goal is for machine readability/usage. Because it is already implicitly using Locale.getDefault()
if you don't specify one, and this can cause hard to find bugs when devices have their own default locale specified. It seems that you also need to clean your project either way, as everyone else has suggested.
Solution 4 - Android
use Locale.getDefault() and than clean your project.
Solution 5 - Android
It's probably a Lint bug. Just try to cut the whole line of code
if (e.getMessage().toLowerCase(Locale.ENGLISH).contains("blabla"))
save, then paste.
Solution 6 - Android
Cleaning the project didn't work for me, so I added the default locale on my code:
String.format(Locale.getDefault(), "firstname: %s, lastname: %s", firstName, lastName));
Depending on your project, you might want to take a look at the Locale explanation.
Solution 7 - Android
As written in https://stackoverflow.com/questions/16927664/why-does-android-lint-warn-about-string-format-using-default-locale-when-explici/ and https://stackoverflow.com/questions/10336730/which-locale-should-i-specify-when-i-call-stringtolowercase some languages like Turkish have different rules of case converting (the 'I' and 'i' characters are not case-converted to one another).
I suppose, this is a bug of Lint rules. Setting Locale.getDefault()
is a good choice. To remove the warning, add before method:
@SuppressLint("DefaultLocale")