NullPointerException tries to access String resource

I have the following /res/values/uris.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="bladder">blahblah</string>
</resources>

I refer to it in code:

private String bladderUrl= getString(R.string.bladder);

But it returns null. I'm not sure why?

+5
source share
3 answers

My guess is that you placed bladderUrl smth as follows:

public class YourActivity extends Activity {
    private String bladderUrl = getString(R.string.bladder);

    @Override
    public void onCreate(Bundle savedInstanceState) {

...

However, you need to do something like this:

public class YourActivity extends Activity {
    private String bladderUrl;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        bladderUrl = getString(R.string.bladder);
...
+21
source

First of all, to understand this. You do not have to have all your lines in strings.xml. Period.

Emmanuels . onCreate() . , , strings.xml, .

+1

, strings.xml.

, . . onCreate().

0

All Articles