0

simple question . I got this

    <?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">YYYYYY</string>
    <string name="leaderBoard">XXXXX</string>
</resources>

in my res>values>strings.xml

Then in my Activity I try to pull out the leaderBoard value but java refuses to do so.

String string = getString(R.string.leaderBoard);

leaderBoard cannot be resolved into a field.

I have tried , cleaning up project , refreshing , still nothing. On the other hand I am able to retrieve app_name into an xml file like the manifest.xml . I m pretty sure that I m blind or something...

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >       
         <meta-data
            android:name="com.google.android.gms.games.APP_ID"
            android:value="@string/app_id" />
...
3
  • is your R.java file is properly updating? Also see the import. And use getResource().getString(R.string.leaderBoard); Commented May 25, 2015 at 8:51
  • Thanks for swift response , yes it was a noobish fault after all , Bojan was correct in my case. Commented May 25, 2015 at 8:57
  • you welcome as i said see your import, well we all make small mistakes after all. Happy coding Commented May 25, 2015 at 9:02

4 Answers 4

2

Make sure you are using the correct R class - the one of your project, not android's. Check the imports.

It should be something like this

import com.yourapp.R;

and not like

import android.R;
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be better explained to simply NOT import android.R. Under normal circumstances there should be no need to explicitly import your own R.java class as it is done implicitly for all classes within the same package.
2

You need to read string form String.xml so you can use getString() which is the method of resource so you will get your output Now.

getResources().getString(R.string.YOUR_XML_STRING);

If not works Please Clean & Rebuild your project

For More Info. You may visit

http://developer.android.com/guide/topics/resources/string-resource.html And http://developer.android.com/reference/android/content/Context.html#getString%28int%29

Comments

0

Use this:

getResources().getString(R.string.leaderBoard);

Comments

0

Change to this

getResources().getString(R.string.leaderBoard);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.