相关文章推荐
狂野的日光灯  ·  wpf - ...·  2 年前    · 
正直的小蝌蚪  ·  113. Spring Boot ...·  3 年前    · 
R._1st couldn't work? If you're talking about Android development, I think the R class is always there... Matthieu Apr 23 '15 at 15:03 @Matthieu I thought so too, until this day when I had to do this very same thing, but only with the BR class. Sevastyan Savanyuk Nov 8 '17 at 16:08

First retrieve the field property of the class, then you can retrieve the value. If you know the type you can use one of the get methods with null (for static fields only, in fact with a static field the argument passed to the get method is ignored entirely). Otherwise you can use getType and write an appropriate switch as below:

Field f = R.class.getField("_1st");
Class<?> t = f.getType();
if(t == int.class){
    System.out.println(f.getInt(null));
}else if(t == double.class){
    System.out.println(f.getDouble(null));
                thanks. I tried but it didn't work. Exception is thrown at the operation f.getInt(null). I caught it but how come there's an exception?
                    – Viet
                Apr 21 '10 at 18:26
                Hi, the Exception e.getMessage() returns the field name, which is "_1st" and nothing else.
                    – Viet
                Apr 21 '10 at 18:41
                But what is the type of the exception? (i.e. NullPointerException, SecurityException, ...)
                    – M. Jessup
                Apr 21 '10 at 19:16
                How come the documentation never mentions that getInt() ignores the passed in argument? Spent hours on trying to get the instance of the class to pass there.
                    – Sevastyan Savanyuk
                Nov 8 '17 at 16:11

Exception handling is left as an exercise for the reader.

Basically you get the field like any other via reflection, but when you call the get method you pass in a null since there is no instance to act on.

This works for all static fields, regardless of their being final. If the field is not public, you need to call setAccessible(true) on it first, and of course the SecurityManager has to allow all of this.

@Viet, can you clarify what didn't work about it? Perhaps post the code that you have that isn't working? – Yishai Apr 21 '10 at 18:37 Hi, the Exception e.getMessage() returns the field name, which is "_1st" and nothing else. – Viet Apr 21 '10 at 18:41

I was following the same route (looking through the generated R class) and then I had this awful feeling it was probably a function in the Resources class. I was right.

Found this: Resources::getIdentifier

Thought it might save people some time. Although they say its discouraged in the docs, which is not too surprising.

So you inferred it was an Android question. Should have been indicated in the tags... – Matthieu Apr 23 '15 at 15:03 It's not an Android question, it's a Java reflection question that uses a particular example. Questions are tagged based on their topic. – Matthew Read Dec 19 '16 at 17:05

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.1.30.35916