Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android/java getIdentifier与_Java_Android - Fatal编程技术网

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