我无法getIdentifier使用类变量.这很奇怪,因为这有效:
public Drawable getStationIcon(Context context) {
int resId = context.getResources().getIdentifier("m1m2_16", "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
public Drawable getStationIcon(Context context) {
int resId = context.getResources().getIdentifier(this.stationIcon, "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
Run Code Online (Sandbox Code Playgroud)
这也不是:
public Drawable getStationIcon(Context context) {
String stationI = this.stationIcon;
int resId = context.getResources().getIdentifier(stationI, "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
Run Code Online (Sandbox Code Playgroud)
而且this.stationIcon绝对等同m1m2_16.我已经尝试了其他替代方案,即使用""+this.stationIcon,但当第一个参数是变量时,没有任何作用.有什么我想念的吗?
奇怪的是它可能会起作用:
getIdentifier("com.mypackage.namehere:drawable/" + this.stationIcon, null, null);
Run Code Online (Sandbox Code Playgroud)
信用: https: //stackoverflow.com/users/790997/idroid
当名称为数字时,Resources.getIdentifier() 会出现意外行为
| 归档时间: |
|
| 查看次数: |
5351 次 |
| 最近记录: |