Tag Archives: Nonnull annotation

[error record] kotlin code compilation error when Android studio compiles (process @ nonnull parameter when upgrading support library)

Contents of articles

1、 Error information 2. Error analysis 3. Solutions

1、 Error information


Recently, we have upgraded all the support libraries to

28.0.0

28.0.0

28.0.0 ;

implementation 'com.android.support:recyclerview-v7:28.0.0'

There are a lot of errors in the support library code of kotlin language;

2、 Error analysis


Here, we inherit the recyclerview.itemdecision class and override the getitemoffsets method,

object : RecyclerView.ItemDecoration() {
                    override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?, state: RecyclerView.State?) {
                        super.getItemOffsets(outRect, view, parent, state)
}

Note that in the overridden method, the parameter type

outRect: Rect?view: View?parent: RecyclerView?state: RecyclerView.State?

All are nullable types;

Look at the real code of itemdeclaration. The four parameters of getitemoffsets method are all non empty types, and the @ nonnull annotation is added. Therefore, non empty parameters must be passed in here. When inheriting, they are nullable parameters. An error will be reported, and the parameter types are inconsistent;

        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            this.getItemOffsets(outRect, ((RecyclerView.LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent);
        }

Item decision complete code reference:

    public abstract static class ItemDecoration {
        public ItemDecoration() {
        }

        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            this.onDraw(c, parent);
        }

        /** @deprecated */
        @Deprecated
        public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent) {
        }

        public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            this.onDrawOver(c, parent);
        }

        /** @deprecated */
        @Deprecated
        public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent) {
        }

        /** @deprecated */
        @Deprecated
        public void getItemOffsets(@NonNull Rect outRect, int itemPosition, @NonNull RecyclerView parent) {
            outRect.set(0, 0, 0, 0);
        }

        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            this.getItemOffsets(outRect, ((RecyclerView.LayoutParams)view.getLayoutParams()).getViewLayoutPosition(), parent);
        }
    }

3、 Solutions


Google did not add the @ nonnull annotation to the parameters of the support library method before. In the

28.0.0

28.0.0

In version 28.0.0, the annotation is added, which leads to the incompatibility of inheritance;

The modification method is to set all parameter types to non empty type and delete the question mark in each parameter;