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?
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.
You simply need to clean your project by clicking:
Build > Clean Project or Build > Rebuild Project
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.
use Locale.getDefault() and than clean your project.
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.
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.
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")
Related
Here is my values\strings.xml (the default file) and everything is self explanatory:
My question(s):
How can it be "not found in default locale" if I am editing the default locale (values\strings.xml)?
How can it be a lint translation error if I set translatable="false"? In the values-pl\strings.xml (and values-ru, values-iw folders as well) the strings don't exist even? it shouldn't anyway.
I can't seem to understand why I don't get the error for the Russian string.
(I would show the values-ru\string.xml or values-pl\string.xml here but there is nothing there of interest, since the strings are missing anyway...)
this happened to me too
I do 2 things:
Verify entire file to see another translate not done ( I have ones in file en but in pt-br)
Clean, update gradle and quit and restart the Android Studio
Rebuilding can take a long time. YMMV, but...
Simply remove the offending line and re-add it.
Worked for me. Very fast.
In my case this helped me:
Select the strings that cause error.
Cut them.
Paste them again
If it doesn't work for you, try adding the same strings to strings.xml(v21) file.
Press Invalidate Caches / Restart ... to restart android studio. It worked for me. Actually this happened when I had copied files from my other computer and pasted via file explorer.
I solved this issue by following below steps:
cleaning my project
rebuilding it
and finally pressing invalid caches/ Restart in file menu
If you just Clean Project it probably works. It worked for me.
I had the same error message, just with the weird issue that it was thrown in the default locale itself and all translations. Turns out you should not use dots in your name (e.g. name="bla.blub") because it will be internally converted to "bla_blub" and then it cannot match with "bla.blub", hence the error.
I only had to change the dots to underlines in the default locale and then all other errors in other translations (including dots in the name) were gone as well.
But be aware that other build tools can still create issues, so replace all the dots with underlines instead!
Quitting Android Studio and restarting it fixed it for me.
You simply just have to copy the offending line (or just one of the multiple of offending lines), remove it, and re-paste it. That removed the issue for me.
Just make sure that you have same naming conventions on all strings files including the same capitalization.
let's say if you have "sign_in" in strings.xml and "sign_In" in your fr/string.xml, so it will give you error on fr/strings.xml like "is translated here but not found in default locale". So, when you edit to "sign_in". The error will remove.
I was having this problem for all the strings in my xml file.
The thing is, your default strings.xml shouldn't have a tools:locale attribute as the other translated files have. If it does, whenever I compile the app in release mode, it treats it as another translated file.
I've created a tool to manage the translation status of android apps.
It not only tells you what strings are missing on the other languages, but it can also report, and clean the left over strings that you may have deleted on your default translation file.
https://github.com/gubatron/android-missing-strings
To clean all left overs in other languages invoke like this
./ams --cleanleftovers -o all.txt
This will clean the left over strings and it will output the missing strings report for all the languages into the all.txt file
Copy and paste didn't work for me.
I also tried Clean and restart, the previous error was gone but new entered strings still have new errors occur.
I tried closing the opened strings.xml file and Translations Editor, then restart.
strings.xml file and Translations Editor
That works for me.
Well, in my case, this happened when there was a format error with the previous string of the string that's being reported. Unfortuantely that format error was not reported. Correcting the format error solves the issue.
Add this,It will work
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="ExtraTranslation">
Press double shift for open Translations Error and then check and uncheck in Untranslatable column the item with error.
I have inherited an old android app, and the first thing I did, was change the build from Ant to gradle.
The app builds fine, but when I try to run the release target, gradle fails complaining about missing translations.
The folder structure of the resource folder is: (the default language, in the values folder is en-GB)
res/
values/
/file1.xml
/file2.xml
/file3.xml
/values-us/
/file2.xml (containing just the few lines that differ from /value/file2.xml
/values-dk/
file1.xml
file2.xml
file3.xml
So, basically the linter is correct, most of the files are missing in the values-us folder.
However, a single locale contains about 20 files, with around 100+ lines each, and the difference between the default and US is probably 5 lines total, it seems impractical to have to maintain all values for both locales.
To be clear, I absolutely want the release target to fail, when values-dk/ or values-de/ are missing translations, I's just like some way to mark the US locale as an extension of the default. Is this possible?
I think Lint found some strings that are not in res/values but in res/values-{language}. In this case, if the code tries to use one of those strings and the string selection falls to default one (phone is set to a language where you don't provide translations), the application will crash.
The error should tell you which string is not translated. I'd start by checking the languages defining it and make sure it's also in the default ones.
So it turns out this is actually pretty simple. All I had to do was rename the values-us/ folder to values-en-rUS/ and it works.
I added this line to my strings.xml, and it wont compile into R.
<string name="cl_txt_verinfo">List Version: %1$s\nAuthor: %2$s\nDate Created: %3$s\nLanguage: %4$s</string>
the errors i get are:
error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
error: Unexpected end tag string
I want to ask how to make this work(i will use string.format later)
and what does "formatted=false" do as opposed to not adding it/add it as true.
Edit:
Clean and build is how I got this error. I got it so many times.
But then I deleted an empty line and clean and rebuild everything is fine.
This is really inconsistent and i have no idea what's causing the problem in the first place.
Two questions left:
Could someone still tell me what difference would "formatted=false"
make?
How did I got this kind of behavior?(reporting error when there's none and i try to fix it by blowing things up and create a mess)
This is not the only time i had an error then it magically fixes itself. sometime i restart my adt and it resolves itself, but not this time sadly.
If you added formatted=false then format specifiers(%,$ etc.) in your string resource are disabled and Formatter won't be able to recognize them. This will definitely help you.
For your 2nd problem there is no exact solution but what I do is delete gen and bin folder and then clean build project. What happens is every time you build a project, android checks if there is any new resource available and adds it to R.java and only after that you can use it.
How can I create a layout file with data and fill them with some example data so that I can see what I have done without producing warnings.
Here is an example:
If I remove the text (which wants Android Lint) I have a plain blue screen, which is not helpful at all.
By the way this is the warning:
[I18N] Hardcoded string "Foobar", should use #string resource example.xml /Example/res/layout Line 42 Android Lint Problem
Create an entry in your strings.xml for each of your example Strings.
On the other hand, this is a Lint warning. You can safely ignore this without causing immediate harm to your app, as these are only meant to guide you in best practices.
Go to the Eclipse preferences, into the Android submenu, then into the Lint Error Checking menu and disable the HardcodedText warning.
Second Method
Instead of setting the example text through XML, use the setText() through Java code, and enclose it in an if condition as follows:
if(BuildConfig.DEBUG) {
view.setText("My example text");
}
BuildConfig.DEBUG was added in ADT 17, and is true whenever you do a debug build. When you export the apk, it is set to false. As the variable is set to false and never reassigned, the Java compiler will optimize your code and remove these statements. They will not be compiled into a production app.
For some reason auto complete no longer works. for example when writing code using the android bitmap class, when i use the dot operator eclipse would suggest methods for that object instance. how do you turn this feature back on?
thanks mat.
I had this same problem. Here is how I solved it.
In Eclipse go to
Window -> Preferences -> Java -> Editor -> Content Assist -> Advanced
And check the boxes labeled "Java Proposals" and hit Apply.
Im my case was API tools proposals the option that cause my autocomplete doesn´t work, here are the 4 default options that must be checked...
API Tools Proposals.
Java Proposals.
SWT Template Proposals.
Template Proposals.
the next time i must be careful when click Ctrl+Space =D
Go to Preferences > Java > Editor > Content Assist and paste "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz." (note the dot after z) into the "Auto activation triggers for Java:" field.
This worked for me ! Now as i type I get autocomplete options
Here's another solution I dicovered:
I would check the other solutions listed here first and if all else fails shut down eclipse then delete the file 'org.eclipse.jdt.ui.prefs' found in the workspace directory (Workspace\.metadata\.plugins\org.eclipse.core.runtime\.settings).
Seems in my case the workspace became corrupted somehow and this fixed it. Haven't yet noticed any adverse effects by doing this.
eclipse use "ctrl+[space]" to show the "css/html/java proposal"
I had the same problem and none of the solutions here worked. For some reason, my Scheme had changed from "default" to "Emacs". So to fix it, I had to go:
Preferences> General> Keys and change the scheme back to default.