我已经试过这样:
r = Resources.getSystem().getIdentifier("ball_red","drawable","com.Juggle2");
Log.i("FindBall","R = "+r);
和这样的:
r = Resources.getSystem().getIdentifier("com.Juggle2:drawable/ball_red", null, null);
不过,R最终总是为零。
But 'r' always ends up as zero.
我打电话这条线从一个辅助类,这不是一个活动,并没有延伸什么里面,所以无法简单地调用 getResources()
,但我可以从我的 SurfaceView
通过。
I'm calling this line from inside a helper class that's not an Activity and doesn't extend anything, so I can't simply call getResources()
, but I can pass it from my SurfaceView
.
最后,我想替换ball_red
有一个变量,但第一件事情第一。这是行不通的。
Eventually, I want to replace "ball_red"
with a variable, but first thing's first. This isn't working.
com.Juggle2
确实是我包的名字。 绘制
是 RES
文件夹,它是在,而且,该文件的名称确实是 ball_red
。
com.Juggle2
is indeed my package name. drawable
is the res
folder that it's in, and, the name of the file is indeed ball_red
.
R.java说:
public static final int ball_red=0x7f020027;
所以我不知道为什么它不工作。
So I'm not sure why it isn't working.
所以,我不能使用的资源,我必须通过一个背景,而我这样做是这样的: 里面的位置:
So I can't use Resources, I must pass a context, and I'm doing that this way: Inside here:
class Collection extends SurfaceView implements SurfaceHolder.Callback {
我做我的类的新实例,并传递给它的getContext()
作为一个参数。
既然你是一个活动里面就足以写
Since you are inside of an activity it is enough to write
int resId=YourActivity.this.getResources().getIdentifier("ball_red", "drawable", YourActivity.this.getPackageName());
或者,如果你不从内部类调用它
or if you're not calling it from an inner class
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是否有绘制
名为 ball_red
Check also in your R.java whether there is a drawable
with the name ball_red
例如:
public static final class drawable {
public static final int ball_red=0x7f020000;
}
修改
如果你没有在任何活动,那么你必须已经过时了一个环境,而不是资源参数
然后执行
EDIT
If you're not in any activity then you must passe a context instead of resources as parameter
then do this
int resId=context.getResources().getIdentifier("ball_red", "drawable", context.getPackageName());