我无法让 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;
}
但这不是:
public Drawable getStationIcon(Context context) {
int resId = context.getResources().getIdentifier(this.stationIcon, "drawable", "com.mypackage.namehere");
Drawable drawable = context.getResources().getDrawable(resId);
return drawable;
}
这也不行:
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;
}
this.stationIcon
绝对等于 m1m2_16
。我已经尝试过其他替代方法,即使用 ""+this.stationIcon
,但是当第一个参数是变量时没有任何效果。有什么我想念的吗?
最佳答案
奇怪的是它可能会起作用:
getIdentifier("com.mypackage.namehere:drawable/" + this.stationIcon, null, null);
来源:https://stackoverflow.com/users/790997/idroid
Resources.getIdentifier() has unexpected behavior when name is numeric
关于java - android/java getIdentifier 与,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4771397/