Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve NonNull annotation after Android Studio 3.2?

After updating to Android Studio 3.2, I've been getting a "Probable bugs" from lint saying:

"Not annotated method overrides method annotated with @NonNull".

I had no issue before updating to Android Studio 3.2, how do I solve this?

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View row = convertView;
    ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        row = LayoutInflater.from(context).inflate(R.layout.gallery_view_row, parent, false);
        holder.imageView = row.findViewById(R.id.filePath);
        row.setTag(holder);
    } else holder = (ViewHolder) row.getTag();

    String file_path = objects.get(position);
    GlideApp.with(context).load(MovieDB.IMAGE_URL + context.getResources().getString(R.string.galleryImgSize) + file_path)
            .into(holder.imageView);
    return row;
}

Android Lint never flagged this method before Android Studio 3.2 update, but now I get 2 flags on the method and on the "parent" parameter

Here are the 2 imports used

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

The problem only exists the the "ArrayAdapter" class, the superclass has those 5 imports highlighted in red

import android.annotation.ArrayRes;
import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
like image 370
Karl Ghosn Avatar asked Sep 30 '18 19:09

Karl Ghosn


People also ask

How to add annotations in android studio?

The first step is to build a new module that will house your annotations. Go to Android Studio and then click on File -> New ->New Module then choose Kotlin and after that, you need to add a name to the module like you usually do with your android projects. Name the module gfg-annotations. Set the package to com.

What is @nullable annotation in Android?

public annotation Nullable. Denotes that a parameter, field or method return value can be null. When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.

What does javax NonNull do?

@NonNull – The compiler can determine cases where a code path might receive a null value, without ever having to debug a NullPointerException. @ReadOnly – The compiler will flag any attempt to change the object. This is similar to Collections. unmodifiableList, but more general and verified at compile time.

How do I add annotations to Android support?

To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.


1 Answers

I was having this same problem, finally discovered a fix, hope it works for you...

Open Settings, select "Editor", then "Inspections".

In the list of inspections, go to "Probable Bugs" and then "@NotNull/@Nullable problems".

On the right pane, select the "CONFIGURE ANNOTATIONS" button.

Add "androidx.annotation.Nullable" and "androidx.annotation.NonNull" to the respective lists. I also made them the default.

No longer seeing this maddening behavior.

Screenshot

like image 148
tliebeck Avatar answered Oct 31 '22 11:10

tliebeck