由于您处于活动中,因此足以编写
int resId = YourActivity.this.getResources().getIdentifier( "ball_red", "drawable", YourActivity.this.getPackageName());
或者如果您不是从内部类调用它
int resourceID = getResources().getIdentifier( "ball_red", "drawable", getPackageName());
注意
getIdentifier() Returns 0 if no such resource was found. (0 is not a valid resource ID.)
检查一下
还要在R.java中检查是否有一个
drawable名称为
ball_red
例如:
public static final class drawable { public static final int ball_red = 0x7f020000; }编辑 如果您不参加任何活动,则必须通过,
context instead of resources as parameter然后执行此操作
int resId = context.getResources().getIdentifier( "ball_red", "drawable", context.getPackageName());



