android/java getIdentifier与
让android/java getIdentifier与,java,android,Java,Android,让getIdentifier处理类变量时遇到问题。这很奇怪,因为这是有效的: public Drawable getStationIcon(Context context) { int resId = context.getResources().getIdentifier("m1m2_16", "drawable", "com.mypackage.namehere"); Drawable drawable = context.getResources().getDrawable
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;
}
而
这个.stationIcon
绝对等于m1m2\u 16
。我尝试过其他方法,例如使用“”+this.stationIcon
,但当第一个参数是变量时,任何方法都不起作用。我有什么遗漏吗?奇怪的是,它可能会起作用:
getIdentifier("com.mypackage.namehere:drawable/" + this.stationIcon, null, null);
学分:
我建议在使用前记录
log.d(TAG,“stationIcon=“+this.stationIcon)
这个this.stationIcon
以确保它确实是“m1m2\u 16”。我已经这样做了,而且它肯定是m1m2\u 16