Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of a warning in Android Studio

Android studio seems to think SparseArray values cannot be null.

When I write

public static void foo() {
    SparseArray<Object> sparseArray = new SparseArray<Object>();
    sparseArray.put(0, null);
    if (sparseArray.valueAt(0) == null)
        Log.d("MyClass", "Hello World");
}

I get the warning

condition 'sparseArray.valueAt(0) == null' is always 'false'

I'd just like to know what annotation or comment I need to put to get rid of the warning. I don't want to disable inspections, just get rid of this particular warning. Thanks.

like image 225
Paul Boddington Avatar asked Aug 22 '15 16:08

Paul Boddington


People also ask

How to ignore lint errors in Android Studio?

To persistently configure which rules are run, you can create a file named lint. xml in the root directory of your project (next to the manifest file). Lint will automatically look at this file and use it to ignore warnings. Gradle, Android Studio and Eclipse will also use this configuration file if present.

What is Android lint?

The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization. When using Android Studio, configured lint and IDE inspections run whenever you build your app.


1 Answers

You can suppress inspections locally with //noinspection <inspectionname>.

For example:

//noinspection ConstantConditions
if (sparseArray.valueAt(0) == null)

gets rid of this false warning.

like image 102
laalto Avatar answered Sep 21 '22 16:09

laalto