问题描述
对三个按钮进行监听,用Id分辨哪一个按钮是哪一个。degub没有error,但是在commit的时候有点tip,然后review了下发现三个Warning!!! - Warning:(40, 22) Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements
作为一个垃圾码农,0Warining是一种习惯。
// 按钮监听方法
class MyClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_back:
Log.d("btn listener:", "btn_back is clicked");
break;
case R.id.btn_forget:
Log.d("btn listener:", "btn_forget is clicked");
break;
case R.id.btn_login:
Log.d("btn listener:", "_login is clicked");
break;
default:
Log.d("btn listener:","something is clicked");
break;
}
}
}
问题查询
大致意思是Id已经在新版本中一种不是final了,而是可以修改的变量。。。。也就是靠资源Id无法辨别谁是谁???我去,那用什么分辨你是你,我是我???
解决办法
谷歌开发者网站一时半会上不去。。。。找不到官方原版就暂时bing一下吧。。
Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case - 世外大帝:三种方案: 强制压制警告、if、手动final
Non-constant Fields in Case Labels - Android Studio Project Site:用if else 替代switch
The solution for this is simple: Convert the switch statement into an if-else statement.
public void onClick(View v) {
int id = v.getId();
if( id == R.id.btn_back){
Log.d("btn listener:", "btn_back is clicked");
}else if( id == R.id.btn_forget){
Log.d("btn listener:", "btn_forget is clicked");
}else if( id == R.id.btn_login){
Log.d("btn listener:","something is clicked");
}
}
参考
- Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case statements - Sufian:原因:这样会build的更快
- Non-constant Fields in Case Labels - Android Studio Project Site:官方解释居然是用if else 替代switch?????阿哲假官方??
- Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them in switch case - 世外大帝:三种方案
- 安卓大变天!ButterKnife被弃用:Resource IDs will be non-final in Android Gradle Plugin version 5.0 - Eternity岚:使用BindView办法解决ButterKnife被弃用,虽然问题不是用一个,但是错误类型相似