简体   繁体   English

为什么要编译此方法?

[英]Why does this method compile?

Why does this method compile? 为什么要编译此方法?

    private int test(){
      return R.string.test;
    }

R.string.test is defined this way in my android strings.xml file: R.string.test是在我的android strings.xml文件中以这种方式定义的:

<resources>

     <string name="test">Test</string>  

</resources>

Everything I know about logic, the universe and life itself currently makes no sense. 我所知道的关于逻辑,宇宙和生命本身的一切目前都毫无意义。 Please help a confused soul. 请帮助一个困惑的灵魂。

定义资源时,Android代码生成器将读取resources文件并生成具有所有resources id的Java文件R.java ,这就是代码正确编译的原因。

R.string.teste get id of string as integer. R.string.teste以整数R.string.teste获取字符串的id。 So i didn't see any problems... 所以我没看到任何问题...

To get string you should write context.getResources().getString(R.string.teste) 要获取字符串,您应该编写context.getResources().getString(R.string.teste)

Try this : 尝试这个 :

private String test(){
    String mess = getResources().getString(R.string.test);
    return mess;
}

In Android, all the resources located in the res folder are compiled in a class called R.java , there, you have an identifier of the resource created. 在Android中,位于res文件夹中的所有资源都在一个名为R.java的类中进行编译,在那里,您具有创建的资源的标识符。 For example, inside of the R.java class there is a sub class called string for the Strings, id for the ids and so on. 例如,在R.java类的内部,有一个名为string的子类(用于Strings),一个id用于ids)等等。 In your example you will have: 在您的示例中,您将具有:

public final class R {

    // Other stuff   

    public static final class string {
        public static final int test=0x7f05001c;
        // More String resources
    }

    // Other stuff

}

So when you are doing return R.string.test; 所以当你做的时候return R.string.test; you are returning the id for that resource, in my example 0x7f05001c 您正在返回该资源的ID,在我的示例中为0x7f05001c

If what you want is retrieve the string itself, instead of its id, you need to do what @Suvitruf told you: context.getResources().getString(R.string.test) 如果要检索的是字符串本身而不是ID,则需要执行@Suvitruf告诉您的操作: context.getResources().getString(R.string.test)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此Java 8方法参考进行编译? - Why does this Java 8 method reference compile? 为什么会这样编译? 覆盖方法不是异常的子类 - Why does this compile? Overriding method not a subclass of exception 为什么这段代码会编译? (java泛型方法) - Why does this code compile? (java generic method) 为什么这个方法引用赋值编译? - Why does this method reference assignment compile? Java 8流,为什么要编译第2部分......或者什么是方法参考,真的吗? - Java 8 streams, why does this compile part 2… Or what is a method reference, really? 你能解释为什么第一个unwrapped方法引用不能编译吗? - Can you explain why the first unwrapped method reference does not compile? 为什么这个涉及通用接口方法的程序编译? - Why does this program involving a generic interface method compile? 为什么在空引用上调用方法成功编译? - Why does invoking a method on a null reference compile successfully? 重新抛出异常:为什么方法在没有throws子句的情况下编译? - Rethrowing an Exception: Why does the method compile without a throws clause? 为什么隐藏的静态方法在Sun JDK 6下编译但在OpenJDK 6和7下导致编译失败? - Why does a hidden static method compile under Sun JDK 6 but cause a compile failure under OpenJDK 6 and 7?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM