New to Android studio and tried out writing some lines for my assignments. But I have noticed that there are too many highlighted areas which I have no idea what they are for since my approach to the assignments seemed correct. I have attached a photo for easy reference, hope someone can clarify.
ChiefTwoPencils already told you the solution for this
Alt+Enter will show you the options for fixing highlights. Try not to suppress warnings nor disable inspections, because especially your case they might have reason.
If you didn't see those highlights before you have probably disabled inspection (Lint) for those cases before updating Android Studio
On your screenshot you have basically 2 problems:
lines 46, 47, 48 -> Method invocation might produce null pointer exception variables btnInterface, btnVariable, btnInner might be null. You probably didn't initialized those variables (btnInterface = <btnInterface class> findViewById(...))
lines 90 and 93 -> hardcoded strings. Use string resources
Hope you are new to Android, because those are not things you should complain about
I have a project that is showing false positives in sourceanalyzer reports. For example, using a static variable named PASSWORD_DIALOG_TAG is reported as a hard coded password even though it's not. I'd like to be able to hide this line or suppress this particular warning in my fortify reports. Note, I don't want to suppress all warnings for hardcoded passwords, just this one.
I know that for this particular warning I could simply change the variable name, but there are other types of warnings that aren't as easily rectified.
I would first confirm it is really a false positive by AWB >> Group By [Category Analyzer]. If the PASSWORD... is found by the Semantic analyzer, then it is high possibility that the issue is due to keyword "PASS/PASSWD/PASSWORD/SSN/TXTNO..." etc.
There are 4 ways to hide an issue.
(1) #AWB suppress it: This is not recommended as it would suppress all current and FUTURE issue under this RULEGUID. However, the issue can be found in the DB and can be reverted.
**(2) #AWB NAI it: ** This is recommended. Click on the Analysis tag and mark it Not an Issue (see above picture)
(3) #AWB Filter it: Top menu bar >>Options >>Show View >>Filter >>Visibility Filter >> Create New filter >> a pop up wizard shows up >> Category contains .
This method is compliment to other methods. After you suppressing/NAIing and issue, you can make the false positive disappear from your list.
(4) exclude certain file from being scanned by command line parameter -exclude /absolute path of dir/file.name (list here for reference only...)
I use many brackets and braces when I code. Be it casting multiple times, casting multiple times in if blocks, etc. I sometimes get lost in the brackets and also, hate putting a lot of them.
Is there any short-cut key to format this selected part of code?
I have tried Ctrl-Shift-F, but that doesn't give me what I want.
Note: I work on Eclipse Mars.
What you want is Source menu | Cleanup, then customize the profile to add the remove extra parenthesis. From the Code Style tab check the Use parenthesis in expressions Only if necessary. And then complete the wizard:
That changes:
super.start(((BundleContext)(context)));
to:
super.start(context);
As an extra you can set your project to do code cleanup tasks on save automatically if you desire.
did you try ctrl+3 and type formate? for me Ctrl-Shift-F was bound by other app and cause eclipse to miss catching it.
search for formatter Java->code style->formatter
There is a tab Braces.
If you select specific lines and press Ctrl + Shift + F eclipse will only format the lines you selected according to your formatter.
I find this extremely annoying that you often do not get help with figuring out the parameter signatures of methods and contructors when you have already written the first parameter.
Instead Intellij will show the variables available to you in your context. This is good, but not the first prio. I want to see the signature of the method/constructor as well.
This has the consequence that you always have to keep deleting and CTRL + SPACE to see the signature.
Is there a way around this issue?
You don't have to press Ctrl+P, you can configure IDEA to always show this information:
Go to Settings > Editor > General > Code Completion
Select the Show full signatures and Autopopup in (ms:) boxes.
You can press Ctrl+P to show the signature(s) of the method/constructor, see this link for more details:
https://www.jetbrains.com/idea/webhelp/viewing-method-parameter-information.html
I have a Java code line where IntelliJ displays a warning. How do I silence the warning in that particular line, without affecting warnings displayed in other lines?
In this question it's irrelevant what the actual warning is: now I'm not seeking advice on how to improve the quality of a specific piece of Java code, but I want to know in general how to prevent IntelliJ from displaying a warning on a specific Java source line.
Mostly in IntelliJ, you can click on the line and Alt+Enter, and it will have options for suppressing the warning, among other things.
Expanding upon Ryan Stewart's answer, in IntelliJ, use Alt+Enter, then select the first sub-menu, then the last item: Suppress for statement.
Update
Using IntelliJ IDEA 13, I noticed an additional menu item: "Suppress for statement with comment". This will use the IntelliJ style //noinspection unchecked. If you select "Suppress for statement", IntelliJ will insert this text: #SuppressWarnings("unchecked").
Depending on the warning you can use #SuppressWarnings. Eg:
#SuppressWarnings("deprecation")
yourLineWhichIsDeprecated;
Take a look at this answer for a pretty long list of warnings you can suppress. Check the other posts on that topic for more details.
In IntelliJ 15 the inline "yellow bulb" and alt-enter menus don't offer to suppress the inspection for 1 line.
There are more options when running the inspections via the Menu: Analyze -> Inspect Code....
Then on the Inspection panel the right side offers various options.
Some of text in the right hand panel is clickable. Note that usually the problem resolution function (quick fix) is also available.
(Apparently #tino already noticed this in a comment, but I missed his comment the first time. I'm adding this as full answer to make the important bit I personally missed easier to find.)
Just one more note. If you are looking for a answer to suppress all warnings for a next line (or part of the code). It might be a reason for a several cases:
Idea doesn't provide error name, or suggestions
Number of warnings for next line is too large
You can use just:
//noinspection ALL
Click on the complaining area that has a wavy line 〰️ beneath it
A light bulb 💡 appears, click the light bulb 💡
Select the suppress statement option
4. An no inspection comment will be added above current line //noinspection CssInvalidPropertyValue
The complaining disappear 👏
Problems panel
The Problems panel shows a list of warnings and errors in our code. There we can peruse the various issues with our code, working through the list one-by-one. This feature arrived in IntelliJ 2020.2.
In at least IntelliJ 2021.2, and perhaps earlier, we can suppress a warning within that panel.
When selecting a problem point, the right-side pane of the Problems panel shows a Suppress widget. This pop-up menu displays items for various ways to suppress the warning.
When compiling code using Kotlin language and IntelliJ here is a hint
Here is a link to the github source where the compiler warnings have their origin and where the default error messages output by the Kotlin compiler are defined
kotlin/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java
If the compiler outputs "Variable ''{0}'' is never used" it origins from this line form DefaultErrorMessages.java
MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);
To suppress the warning you can put a #Suppress() annotation before an unused variable in your code like this:
#Suppress("UNUSED_VARIABLE")
var y: Int = 3
So the trick if the IntelliJ does not help you pop up suggestions pressing Alt+ENTER at a highlighted expression is to look inside DefaultErrorMessages.java if you can find the error message and the keyword to supress a particular warning using #Suppress(..names)
This topic is not marked "Kotlin" but at least marked IntelliJ
//noinspection unchecked,ConstantConditions
#SuppressWarnings does not work in every place
As other questions point out, there are several options to achieve what are you asking for. A more comprehensive list of the options available can be found in the official documentation here.