0

I'm tring to get an id of the picture by name someId = "hd_2" Resources.getSystem().getIdentifier(someId, "drawable", getPackageName());

public static final class drawable {
    ...
    public static final int hd_2=0x7f020088;
    ...
}

Why it might return java.lang.IllegalArgumentException : Invalid method?

update:

I moved Resources.getSystem().getIdentifier from onCreate to onStart, now it returns 0.

2 Answers2

1

From the documentation on Resources.getSystem():

Return a global shared Resources object that provides access to only system resources (no application resources), and is not configured for the current screen (can not use dimension units, does not change based on orientation, etc).

You are looking in the wrong Resources object. You need to get the instance from your current Context. Something like:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Resources res = getResources();
    int resId = res.getIdentifier(someId, "drawable", getPackageName());
}
devunwired
  • 62,780
  • 12
  • 127
  • 139
0

Maybe this can help you: https://stackoverflow.com/a/15488321/3633665

int resId=YourActivity.this.getResources().getIdentifier("res_name", "drawable", YourActivity.this.getPackageName());
Community
  • 1
  • 1
stkotok
  • 101
  • 1
  • 5