1

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?

Alon Shlider
  • 790
  • 7
  • 25

3 Answers3

2

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.

Gabriele Mariotti
  • 192,671
  • 57
  • 469
  • 489
  • I have tried your solution and here is my line of code - ' public String BASE_URL = getResources().getString(R.string.hero_apps_api); ' but still getting a NPE. What am I missing? I am declaring the variable before onCreate, if it matters. – Alon Shlider Aug 27 '19 at 19:22
  • 1
    alright, answered to myself. I need to put it inside the lifecycle. Thanks! works. – Alon Shlider Aug 27 '19 at 19:23
1

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);
tiborK
  • 362
  • 1
  • 6
  • your solution gives the following error - 'NPE - Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference' – Alon Shlider Aug 27 '19 at 19:06
  • If you are getting it from the resource file you don't need to assign it to a constant. Retrieve it when you need it. – tiborK Aug 27 '19 at 19:19
0

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)
Tamir Abutbul
  • 6,472
  • 7
  • 16
  • 44
  • your solution gives the following error - 'NPE - Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference' – Alon Shlider Aug 27 '19 at 19:06
  • I was sure that you are using activity. Are you using a fragment ? if so check [this thread](https://stackoverflow.com/questions/14112628/access-to-getstring-in-android-support-v4-app-fragmentpageradapter) – Tamir Abutbul Aug 27 '19 at 19:08
  • I am using a string from the resouce file and when trying your method I got the NPE I posted to your comment – Alon Shlider Aug 27 '19 at 19:09
  • I am using an activity – Alon Shlider Aug 27 '19 at 19:09
  • My code works fine for me, have you checked [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Tamir Abutbul Aug 27 '19 at 19:18