George Papatheodorou
George Papatheodorou

Reputation: 1559

Unable to retrieve string value from strings.xml to Java

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" />
...

Upvotes: 0

Views: 947

Answers (4)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

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;

Upvotes: 2

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75798

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

Upvotes: 2

payal tuteja
payal tuteja

Reputation: 170

Change to this

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

Upvotes: 0

Tony Vu
Tony Vu

Reputation: 4371

Use this:

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

Upvotes: 0

Related Questions