I want to change targetSdkVersion of my android project from 25 to 27.
The problem is that i used Kotlin and there are some big changes:
1) changed the signature of View.findViewById(). Now I need to use findViewById<View>
2) getContext() of Fragment became not null safety so I need to add !! to all context values in all fragments.
Is there any way to fix it automatically for whole project? This is not a small project and do this fixes manually will take a lot of time.
There's not an easy way built in to migrate findViewById that I know of. I believe I recorded a Vim (IdeaVim) macro to help me when I did it.
The nullability of getContext() did not change, only the annotation did. In the past getContext() could have returned null, the compiler just didn't warn you. You should not try to find a way to ignore these errors, or even to add !! to each usage. You should instead handle the case where the return value is null and do the right thing in your application.
Related
When I try to use the methods .startsWith and .contains with the variable value, I get the words in red, because apparently, they do not exist.
Can someone help with this error? I tried looking on the internet to see if this is an old deprecated function but I did not find anything apparently it is not deprecated...
Click to see the error here
I think you are leaving an space after calling the contains function. So is like you are not sending any arguments to the function.
It should be like this:
value.contains("/")
And same for startsWith
value.startsWith("-")
I just updated my old Android studio from 4.1.2 to the new ARTIC FOX 2020.3.1... and the error magically disappeared...Thanks.
I'm an Android dev who is using AndroidStudio or IntelliJ IDEA.
I tend to trust my IDE and I'm annoyed with next facts:
In android project when IDE autogenerates a method in java that extends Kotlin class then both IDE uses #NotNull instead of #NonNull, is there setting for this? This
We have #ParametersAreNonnullByDefault but when I override the method from the point 1 and I don't put any annotation IDE warns me, but why?
Am I wrong in my assumptions?
Are there solutions?
Which annotations to use for null/not-null is set under Configure annotations... in the Compiler page of the Settings/Preferences dialog. Select the one you want and press the checkmark button. See https://www.jetbrains.com/help/idea/nullable-notnull-configuration-dialog.html for documentation.
I can't test right now whether IDEA/AS use the default annotations from there when overriding a method which already uses another, but if they don't you'll need to file a ticket.
I'm working currently on an android project and I have no plans in mind whatsoever to translate it to other languages, so I'm not saving string literals in strings.xml. However Android Studio keeps complaining everytime I hard code a string literal especially when setting the text value for a TextView.
Is there a way to disable these warnings?
you can edit it in the following location Settings->Editor->Inspections->Android Lint->TextView Internationalization:
and for the xml Settings->Editor->Inspections->Android Lint->Hardcoded Text:
The best way to do this in my opinion is to use a gradle file, this will allow you to suppress these globally without having to do it in Android Studio, so your settings can go into source control as well, and then you don't have to individually decorate each method you want to apply the warning to. To do this, disable SetTextI18n in your gradle files lint options as follows:
android {
lintOptions{
disable 'SetTextI18n'
}
}
Gradle sync and voila, warnings gone.
Add
#SuppressLint("SetTextI18n")
on top of your function(s).
Example:
#SuppressLint("SetTextI18n")
private void updateEZWBMode2ConnectionStatus(){
switch (IsConnected.checkMode2(mContext)){
case IsConnected.USB_TETHERING:
M2StatusTV.setText("Status: Connected");
M2StatusTV.setTextColor(Color.argb(255,0,255,0));
M2ReceiveTV.setVisibility(View.VISIBLE);
startTestReceiverSafe(M2ReceiveTV);
break;
case IsConnected.USB_CONNECTED:
M2StatusTV.setText("Status: No Tethering");
M2StatusTV.setTextColor(Color.argb(255,255,51,51));
M1ReceiveTV.setVisibility(View.GONE);
M2ReceiveTV.setVisibility(View.GONE);
stopTestReceiverSafe();
break;
case IsConnected.USB_NOTHING:
M2StatusTV.setText("Status: No USB Connection");
M2StatusTV.setTextColor(Color.argb(255,255,51,51));
M1ReceiveTV.setVisibility(View.GONE);
M2ReceiveTV.setVisibility(View.GONE);
stopTestReceiverSafe();
break;
}
}
Old question, but anyway the accepted answer is misleading.
There is no need to translate string literal resources. In fact, they can be marked as non-translatable in the resources file. This way, you'll still adhere to best practices while not being annoyed by lint and translations.
<string name="invite_sent" translatable="false">Invite sent</string>
While disabling Lint my be useful to stop being annoyed, there is a further reason you want to (and should) use string literal resources: repetition. Following the DRY (Don't Repeat Yourself) principle can avoid a miriad of problems down the line, from complex refactors to unexpected behaviour, as well as inconsistency in the user(s) experience while using the app. Just imagine that an "Ok" button is present in 10+ screens. Having a single reference and source simplifies and centralizes maintenance of the project.
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")
I am using Eclipse Indigo for Android development. The problem i face is that it does not allow #Override for non Activity overwritten methods. For example if i implement onErrorListner of MediaPlayer and i set attribute #Override with it then it gives the following compile time error:
The method onError(MediaPlayer, int, int) of type MyActivity must override a superclass method
And to fix this problem, i am suggested the following
Remove "#Override" annotation
Tough removing the #Override fixes the issue but why does it complain about it and also removing it may cause stopping some functionality of its parent etc??
I have downloaded many examples which use this "#Override" attribute with non-activity methods which proves that this is used and i might be missing some obvious thing. But i cannot run these examples in Eclipse Indigo without removing these "#Override" attributes from all the classes.
Why?
Any help is greatly appreciated.
Your project Java compiler level is set to 1.5 instead of 1.6. See here.
It is not good to remove that line. you need to change to JDK version in your eclipse then you will not get such errors. Follow, following steps for it,
Right Click on your Project & select Properties.
Then from the left section Select Java Compiler & select the Version of the JDK you installed. If it is already selected then de-select it & try it.