简体   繁体   English

Android Studio 3.2之后如何解决NonNull注释?

[英]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: 更新到Android Studio 3.2之后,我从lint那里得到了一个“可能的错误”:

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

I had no issue before updating to Android Studio 3.2, how do I solve this? 在更新到Android Studio 3.2之前我没有遇到任何问题,我该如何解决这个问题?

@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 Android Lint在Android Studio 3.2更新之前从未标记过此方法,但现在我在方法和“parent”参数上获得了2个标记

Here are the 2 imports used 这是使用的2个进口

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 问题只存在于“ArrayAdapter”类中,超类将这5个导入以红色突出显示

import android.annotation.ArrayRes;
import android.annotation.IdRes;
import android.annotation.LayoutRes;
import android.annotation.NonNull;
import android.annotation.Nullable;

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". 在检查列表中,转到“可能的错误”,然后转到“@NotNull / @Nullable问题”。

On the right pane, select the "CONFIGURE ANNOTATIONS" button. 在右侧窗格中,选择“配置注释”按钮。

Add "androidx.annotation.Nullable" and "androidx.annotation.NonNull" to the respective lists. 将“androidx.annotation.Nullable”和“androidx.annotation.NonNull”添加到相应的列表中。 I also made them the default. 我也把它们作为默认值。

No longer seeing this maddening behavior. 不再看到这种令人抓狂的行为。

截图

Any method parameter where this warning appears, annotate it with @NonNull. 出现此警告的任何方法参数,使用@NonNull对其进行注释。 Any method that you're overriding where this appears, do the same (on the line either over or under @Override). 你所覆盖的任何方法都是相同的(在@Override之上或之下的行上)。

Add the @NonNull annotation to your method: 将@NonNull注释添加到您的方法:

@NonNull
@Override
public void myOverridenMethod() {
    //your code
}

Or just ignore it. 或者只是忽略它。

"Not annotated method overrides method annotated with @NonNull". “未注释的方法会覆盖使用@NonNull注释的方法”。

It's pretty clear. 很清楚。

You probably should remove one of those @NonNull from the method (OR maybe those who are already added in the method and just making the method @NonNull ), So: 您可能应该从方法中删除其中一个@NonNull (或者可能是那些已经在方法中添加并且只是创建方法@NonNull ),所以:

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent)

This will tell that the getView can't be null with any of those parameters inside. 这将告诉getView不能为null其中包含任何这些参数。

尝试使缓存无效并重新启动Android Studio。

I have same problem because my android gradle tools too old(2.x) just upgrade project android gradle tools to 3.x solve this. 我有同样的问题,因为我的android gradle工具太旧(2.x)只是升级项目android gradle工具到3.x解决这个问题。

dependencies {
    classpath 'com.android.tools.build:gradle:3.2.1'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM