Quantcast
Channel: Null pointer exception while loading from strings.xml in Android - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by David for Null pointer exception while loading from strings.xml in Android

$
0
0

You should use

int [] stringIds = { R.string.q1, R.string.q2... }

As when you declare the string [], you call getString(id), where some context or resource is needed, but not yet initialized, that accounts for the null error.

e.g.

getResources().getString(R.string.q1); //here getResources() may be null.

Then after your context (e.g. activity) is created, call below function

for( int id : stringIds)
    String question = getResources().getString(id); //now this is no longer null

The difference is R.string.q1 always accessible, either at compile time or at runtime.

Hope this helps.


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>