Android 无法使用getResources()获取资源。getIdentifier

Android 无法使用getResources()获取资源。getIdentifier,android,Android,我需要使用它的名称获取一个drawable,这样我就可以在imageView.setImageDrawable()中设置它,它取int值 我使用的字符串是,img\u state\u btn 代码: 我收到了正确的包名,但id的值显示为0 我哪里做错了 注意-不想使用R.java请按以下方式尝试: 您可以通过下面的代码获得带有名称的图像id int id= getResources().getIdentifier(imageName,"drawable", getPackageName());

我需要使用它的名称获取一个drawable,这样我就可以在
imageView.setImageDrawable()
中设置它,它取int值

我使用的字符串是,
img\u state\u btn

代码:

我收到了正确的包名,但id的值显示为
0

我哪里做错了

注意-不想使用R.java

请按以下方式尝试:

您可以通过下面的代码获得带有名称的图像id

 int id= getResources().getIdentifier(imageName,"drawable", getPackageName());
尝试如下:

您可以通过下面的代码获得带有名称的图像id

 int id= getResources().getIdentifier(imageName,"drawable", getPackageName());

您也可以直接设置

imgView.setImageResource(R.drawable.img_state_btn);

您也可以直接设置

imgView.setImageResource(R.drawable.img_state_btn);

要从名称中获取可绘制id,请使用以下代码。我认为问题是您正在传递null而不是“drawable”


要从名称中获取可绘制id,请使用以下代码。我认为问题是您正在传递null而不是“drawable”

(或)

JavaDoc用于此

int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)



public int getIdentifier (String name, String defType, String defPackage) 
Added in API level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here. 

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Parameters
name  The name of the desired resource. 
defType  Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type. 
defPackage  Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. 

Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.) 
(或)

JavaDoc用于此

int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)



public int getIdentifier (String name, String defType, String defPackage) 
Added in API level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here. 

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Parameters
name  The name of the desired resource. 
defType  Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type. 
defPackage  Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. 

Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.) 

检查图标名称,它应该与..R.identifier name.匹配,然后返回正确的值..检查图标名称,它应该与..R.identifier name.匹配,然后返回正确的值..我使用此方法仅用于资源。getIdentifier(“female.png”,“drawable”,mContext.getPackageName())但仍然无法找到资源。知道有什么不对吗?我只使用这个方法来查找resources.getIdentifier(“female.png”,“drawable”,mContext.getPackageName()),但仍然找不到资源。你知道会出什么问题吗?
int android.content.res.Resources.getIdentifier(String name, String defType, String defPackage)



public int getIdentifier (String name, String defType, String defPackage) 
Added in API level 1
Return a resource identifier for the given resource name. A fully qualified resource name is of the form "package:type/entry". The first two components (package and type) are optional if defType and defPackage, respectively, are specified here. 

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Parameters
name  The name of the desired resource. 
defType  Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type. 
defPackage  Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package. 

Returns
int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)