Home » Android » android – In-line annotations give syntax errors

android – In-line annotations give syntax errors

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m loving the new Lint API checks of ADT rev 17, but the new API Correctness Check has got me stumped. I have the following line of code:

listView.setOverScrollMode(OVER_SCROLL_NEVER);

Lint is reporting on this line:

Call requires API level 9 (current min is 4)

According to the documentation, I should just be able to add an annotation above the line, like so:

@TargetApi(9)
listView.setOverScrollMode(OVER_SCROLL_NEVER);

This, however, gives a syntax error in Java 1.6:

Syntax error on token(s), misplaced construct(s)

How to&Answers:

That’s not allowed in Java (until/if JSR 308 gets added); you can only annotate classes, methods, fields, parameters and variable declarations. It’s the latter that is shown in the docs. However, for bytecode based checks like the api check you may need to place it on a method or class (or anonymous/inner class). The Add Annotation quickfix for these warnings in lint should do the right thing.