I have a class that extends application. I need to get a string from the strings.xml file.
I’m using this class to manage global state, so it is being loaded via the manifest.
Upon load, the getString() generates a null pointer reference.
How can I access the strings from this class?
public class MyApp extends Application {
public MyApp() {
try {
String apiurl = getString(R.string.api_url);
}
catch (Exception e) {
Log.v("MyApp", e.toString());
}
}
}
You need to override the
onCreatefunction rather than use a constructor.So…
http://developer.android.com/reference/android/app/Application.html#onCreate()