70

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?

Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56

7 Answers7

115

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.

Gabriel Netto
  • 1,818
  • 1
  • 16
  • 26
  • `toUpperCase(Locale.getDefault());` and now i get `Locale cannot be resolved` – carbonr Jul 19 '13 at 22:44
  • 1
    Don't the string functions just use Locale.getDefault() internally if you don't specify a locale? – Edward Falk Feb 03 '16 at 22:24
  • What a reason to do that if it's used by default inside toUpperCase() method: public String toUpperCase() { return CaseMapper.toUpperCase(Locale.getDefault(), this, count); } – mc.dev Feb 25 '16 at 17:41
  • Hi, I don't know why "using the default locale is a common source of bugs" any bug case about that? – Gohan Jun 29 '16 at 09:11
34

You simply need to clean your project by clicking:

Build > Clean Project or Build > Rebuild Project

Prince Vegeta
  • 719
  • 6
  • 21
Mahorad
  • 1,258
  • 1
  • 15
  • 22
  • 3
    see my comment to previous answer – Alex Zaitsev Nov 20 '12 at 10:40
  • 2
    Can you please explain "how" cleaning project removes those warnings and "why" user shouldn't do something to solve those warnings and rather just clean the project and move on? – DroidDev Jul 20 '15 at 12:56
  • 1
    @DroidDev You should read the question: "So I try: if (e.getMessage().toLowerCase(Locale.ENGLISH).contains("blabla")) But error still remained! How fix this?" – The incredible Jan Feb 16 '18 at 10:55
6

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.

Dandre Allison
  • 5,975
  • 5
  • 42
  • 56
3

use Locale.getDefault() and than clean your project.

2

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.

Farmer Joe
  • 6,020
  • 1
  • 30
  • 40
yazjisuhail
  • 357
  • 3
  • 6
  • 1
    I also suppose it's a bug of Lint rules. But setting `Locale.ENGLISH` is not a good choice, because other languages may have different rules for case converting. See https://stackoverflow.com/questions/10336730/which-locale-should-i-specify-when-i-call-stringtolowercase and https://stackoverflow.com/questions/16927664/why-does-android-lint-warn-about-string-format-using-default-locale-when-explici/. – CoolMind Feb 02 '21 at 09:15
2

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.

Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68
0

As written in Why does Android Lint warn about String.format using default locale when explicitly using Locale.US? and Which Locale should I specify when I call String#toLowerCase? 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") 
CoolMind
  • 26,736
  • 15
  • 188
  • 224