Android ADT 21.0 warning: Implicitly using the default locale
Asked Answered
B

7

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?

Bremser answered 18/11, 2012 at 21:2 Comment(0)
B
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.

Blaeberry answered 18/11, 2012 at 21:25 Comment(4)
toUpperCase(Locale.getDefault()); and now i get Locale cannot be resolvedMegganmeggi
Don't the string functions just use Locale.getDefault() internally if you don't specify a locale?Orphanage
What a reason to do that if it's used by default inside toUpperCase() method: <code> public String toUpperCase() { return CaseMapper.toUpperCase(Locale.getDefault(), this, count); } </code>Fount
Hi, I don't know why "using the default locale is a common source of bugs" any bug case about that?Sophistic
A
34

You simply need to clean your project by clicking:

Build > Clean Project or Build > Rebuild Project

Allgood answered 20/11, 2012 at 10:37 Comment(3)
see my comment to previous answerBremser
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?Superclass
@Superclass You should read the question: "So I try: if (e.getMessage().toLowerCase(Locale.ENGLISH).contains("blabla")) But error still remained! How fix this?"Parma
F
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.

Family answered 8/3, 2013 at 19:12 Comment(0)
I
3

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

Inductor answered 4/3, 2013 at 6:45 Comment(0)
K
3

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.

Krenn answered 19/7, 2016 at 17:3 Comment(0)
B
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.

Bleach answered 8/5, 2014 at 21:29 Comment(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 #10337230 and #16928164.Logistic
L
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") 
Logistic answered 2/2, 2021 at 9:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.