Android-由switch-case和view.getId()引起的bug:错误:需要常量表达式 的解决办法

无意中,使用按钮测试点击事件,在此过程中使用了switch case语句,在条件switch中使用了view.getId(),出现错误。我记得很早之前写Android程序没啥问题,于是研究了一番。
度娘一番才知道,是因为R文件的缘故。

在Android Gradle Plugin version 8.0中,资源id默认为非final,避免在switch case语句中使用它们。

解决办法有以下:

方法一:

将 switch case 语句换为 if else 语句。

在配置文件中加入 android.nonFinalResIds=false,忽略该错误,但是损失编译效率性能。

同时switch case会提示warning警告。

可以加上@SuppressLint("NonConstantResourceId") 注解消除 warning警告。

相关推荐
shankss1 天前
GetX 状态管理详解
android·flutter·ios
坚持学习前端日记1 天前
原生Android开发与JS桥开发对比分析
android·开发语言·javascript
、、、、南山小雨、、、、1 天前
LCEL基本使用和高级使用
android·服务器·windows
Android-Flutter1 天前
android compose CheckBox, RadioGroup 使用
android·kotlin
ljt27249606611 天前
Compose笔记(六十六)--ModalNavigationDrawer
android·笔记·android jetpack
Android-Flutter1 天前
android compose Tab(顶部) 使用
android·kotlin
方白羽1 天前
Kotlin object 单例设计:为何选择饿汉式而非懒汉式?
android·app·客户端
fundroid1 天前
使用 Gradle Convention Plugins(约定插件)优化 Android 编译配置
android·gradle·约定插件
Jomurphys1 天前
Compose 封装 - 倒计时消失容器
android
城东米粉儿1 天前
android activity启动流程 学习笔记
android