简体   繁体   中英

Integer resource not returning the correct value

In my integer.xml I have

<integer name="minUNameLen">6</integer>

And in my code I am :

if (uName.trim().length() < R.integer.minUNameLen) {
                Toast.makeText(
                        Splash.this.getApplicationContext(),
                        "Should be Min "+R.integer.minUNameLen+" characters long",Toast.LENGTH_LONG).show();

But instead of returning 6 I am getting 2131165…. a weird number in my code. Can anybody tell what's wrong in here?


Doing Resources.getInteger(R.integer.minUNameLen) from here gives me Cannot make a static reference to the non-static method getInteger(int) from the type Resources

That's the resource ID you're seeing.

You need to use

getResources().getInteger(R.integer.minUNameLen)

使用getResources().getInteger(R.integer.minUNameLen)获取值

您得到的是变量R.integer.minUNameLen的值。您需要的是: Activity.getResources().getInteger(R.integer.minUNameLen)

You need a Context object instance to retrieve a Resources object instance and finally get the desired resource, an Integer in your case.

myActivity.getContext().getResources().getInteger(R.integer.yourIntegerID);

as Activity extends Context you can simply call

myActivity.getResources().getInteger(R.integer.yourIntegerID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM