【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( 升级支持库时处理 @NonNull 参数 )

简介: 【错误记录】Android Studio 编译时 Kotlin 代码编译报错 ( 升级支持库时处理 @NonNull 参数 )

文章目录

一、报错信息

二、报错分析

三、解决方案





一、报错信息


最近处理支持库 , 将所有的支持库都升级到了 28.0.0 28.0.028.0.0 ;


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


凡是 Kotlin 语言涉及到的支持库代码 , 出现了一堆报错 ;


image.png






二、报错分析


此处继承了 RecyclerView.ItemDecoration 类 , 重写了 getItemOffsets 方法 ,



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


注意重写的方法中 , 参数类型


outRect: Rect?

view: View?

parent: RecyclerView?

state: RecyclerView.State?

都是可空类型 ;



查看 ItemDecoration 真实代码 , 其中的 getItemOffsets 方法的四个参数都是非空类型 , 添加了 @NonNull 注解 , 因此这里必须传入非空参数 , 继承时继承为可空参数 , 肯定报错 , 参数类型不一致 ;


   

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);
        }



ItemDecoration 完整代码参考 :


 

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);
        }
    }






三、解决方案


Google 在之前的支持库方法的参数中 , 没有添加 @NonNull 注解 , 在 28.0.0 28.0.028.0.0 版本中 , 添加了该注解 , 导致了继承不兼容的情况 ;


修改方法是将所有的参数类型都设置为非空类型 , 删除每个参数中的问号即可 ;


目录
相关文章
|
4天前
|
Android开发
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
Android JNI 报错(signal 6 (SIGABRT), code -1 (SI_QUEUE), fault addr )
26 1
|
Android开发 Kotlin
Android Studio 制作聊天界面实践(Kotlin版)
Android Studio 制作聊天界面实践(Kotlin版)
521 0
Android Studio 制作聊天界面实践(Kotlin版)
|
3天前
|
安全 编译器 API
Android HAL深入探索(5): 调试HAL报错与解决方案
Android HAL深入探索(5): 调试HAL报错与解决方案
6 1
|
17天前
|
Android开发 开发者
安卓投屏神器 Scrcpy安 报错ERROR: Could not find any ADB device
使用Scrcpy安卓投屏工具时遇到报错,问题根源是未开启开发者模式。解决步骤:进入设置,点击【关于手机】→连续点击版本号激活开发者模式,然后在【系统设置】→【开发者选项】中开启USB调试。参照此方法后可正常执行。Scrcpy软件下载链接和GitHub页面也已提供。
26 1
|
XML 缓存 IDE
解决Android Studio报错:Compilation is not supported for following modules
本文主要解决和"Compilation is not supported for following modules"有关的报错。
1437 0
解决Android Studio报错:Compilation is not supported for following modules
|
6月前
|
开发工具 Android开发 iOS开发
使用xamarin开发Android、iOS报错failed to open directory: 系统找不到指定的文件
使用vs2019学习xamarin时,创建新程序。使用模拟器真机等测试都报错如下图错误: ![请在此添加图片描述](https://developer-private-1258344699.cos.ap-guangzhou.myqcloud.com/column/article/5877188/20231030-de8ce5fd.png?x-cos-security-token=r4KyZDEowPT0kGTL0LqE8EnwfN1Nzexadb05dcffed3939ff8d7591c528c01706nvpGSE93QwHpZM8NwhJNTZctNRQa0l3KDhEnqj8P7d8t
63 0
使用xamarin开发Android、iOS报错failed to open directory: 系统找不到指定的文件
|
8月前
|
数据安全/隐私保护 Android开发
uniapp vue3版本 Android 引用 jsencrypt加密库 报错问题 “default“ is not exported by,解决方法
uniapp vue3版本 Android 引用 jsencrypt加密库 报错问题 “default“ is not exported by,解决方法
839 0
|
9月前
|
前端开发 Android开发
在android studio中启动模拟器的时候报错...keeps shopping
在android studio中启动模拟器的时候报错...keeps shopping
67 0
|
11月前
|
Android开发
Android关于Manifest清单文件name报错
在清单文件的根目录加上package 里面的是你的包名 activity的name,前面有个点是接上了你的package名,省略了包名,不然 com.yyt.accountbooks.activity.LoginActivity这样就是不简写的。
61 0
|
11月前
|
数据可视化 开发工具 Android开发
Android Git 拉取代码报错:error: cannot pull with rebase: Your index contains uncommitted changes.
造成原因:在使用Android Studio中Git的Commit Directory 将本地更改的代码保存到本地后,点击commit,发现提交不上去,发现本地有代码,这时候拉取代码,报错:cannot pull with rebase: Your index contains uncommitted changes.表示我的索引有未提交的改变