Alon Shlider
Alon Shlider

Reputation: 1298

Unknown class R.string ... when extracting string resource

I am trying to extract a string resource out of my activity but get the following error -

error

I don't remmber in the past that there was anything that I should have filled before the dot of the "getString" method for extracting a string out of an activity. What am I missing?

Upvotes: 1

Views: 324

Answers (3)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363965

You can use:

String myValue = getResources().getString(R.string.mystring);

but getResources() is a method of the Context class.

It means that you can't do this:

private static String BASE_URL = getResources().getString(R.string.myurl); //it DOESN'T work!!

You can use your BuildConfig class to set these kind of values.
Also in your build.gradle file you can config them:

buildTypes {
    release {
      //..
      buildConfigField("String", "BASE_URL", "....")
    }
    //....
}

It will populate BuildConfig.BASE_URL.

Upvotes: 2

tiborK
tiborK

Reputation: 385

If you want to use another constant declared in another activity make sure that is declared public and import it.

If you wand to use a string declared in the string resource file:

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

Upvotes: 1

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

Try to call getResources() like this:

Java

String yourString = getResources().getString(R.string.herro_app_aoi);

kotlin

val yourString = resources.getString(R.string.herro_app_aoi)

Upvotes: 0

Related Questions