Android getResources / getIdentifier没有返回ID

时间:2013-05-28 13:15:43

标签: android

我的代码类似于:

String s = "replace__menu__" + data.imageid + ".png";
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable-hdpi", getPackageName());

String s = 指令设置的值与res / drawable-hdpi文件夹中的名称之一相同。但是,返回值将 RID 设置为值 0

知道为什么我的代码无效吗?我做错了吗?

2 个答案:

答案 0 :(得分:6)

试试这个

String s = "replace__menu__" + data.imageid;  // image name is needed without extention
int RID = this.getApplicationContext().getResources().getIdentifier(s, "drawable", getPackageName());

答案 1 :(得分:3)

".png" is not part of a ressource name
"drawable-hdpi" I would try just 'drawable' instead