1

I have value inside my code:

val mainFileName = "Untitled list"

As I know the best way to handle strings is using string resource, so inside String resource file I created this string:

<string name="untitled_list">Untitled list</string>

In order to test if i done everything right, I tested that string on toast and everything is working fine:

Toast.makeText(this, R.string.untitled_list,Toast.LENGTH_SHORT).show()

Then I tried to assign to "mainFileName" that string resource by doing this (inside MainActivity class but outside of method OnCreate):

val previewOfNewListTitle = resources.getString(R.string.untitled_list)

but I get an error:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
    at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
    at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
    at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
    at lt.tetro.myapplication.MainActivity.<init>(MainActivity.kt:121)

How can I assign string resource text to "mainFileName" inside of MainActivity class?

3 Answers 3

2

resources needs a valid Context (although you can omit it inside an activity class) like:

this.resources

which does not exist before super.onCreate() is completed.
So every attempt to use it before will lead to an error.
What you can do is declare the variable with the use of the function lazy like this:

val previewOfNewListTitle: String by lazy { resources.getString(R.string.untitled_list) }

This is valid because it does not initialize the variable until the first time you use it in your code.

Sign up to request clarification or add additional context in comments.

Comments

2

Then I tried to assign to "mainFileName" that string resource by doing this (inside MainActivity class but outside of method OnCreate):

You have to wait until after super.onCreate() before attempting to use most methods that you inherit from Activity, including getString().

How can I assign string resource text to "mainFileName" inside of MainActivity class?

Use lateinit var:

class SomethingActivity : AppCompatActivity() {
  private lateinit var mainFileName: String

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    mainFileName = getString(R.string.untitled_list)

    // TODO rest of your activity here
  }
}

Comments

0

resources() is a method of the Context class. If you are inside a Activity or a Service (which extend Context) you can use it like in this snippet.

Also note that the whole language dependency can be taken care of by the android framework. Simply create different folders for each language. If english is your default language, just put the english strings into res/values/strings.xml. Then create a new folder values-ru and put the russian strings with identical names into res/values-ru/strings.xml. From this point on android selects the correct one depending on the device locale for you, either when you call getString() or when referencing strings in XML via @string/mystring. The ones from res/values/strings.xml are the fallback ones, if you don't have a folder covering the users locale, this one will be used as default values.

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.