Quantcast
Viewing all articles
Browse latest Browse all 5

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

You're calling getResources() from a field initializer. Something like

private String mStr = getResources().getString(...);

(or with drawables or other kind of resources).

This is not valid, since the Context isn't setup yet. Move this assignment inside the onCreate() method.

In your case:

String[] questions;

and In onCreate();

questions = { 
            getResources().getString(R.string.q1),
            getResources().getString(R.string.q2),
            getResources().getString(R.string.q3),
            getResources().getString(R.string.q4),
            getResources().getString(R.string.q5),
            getResources().getString(R.string.q6),
            getResources().getString(R.string.q7),
            getResources().getString(R.string.q8),
            getResources().getString(R.string.q9),
            getResources().getString(R.string.q10)
          }

Viewing all articles
Browse latest Browse all 5

Trending Articles



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