Reputation: 1771
I am using this code to pull my string array from my values folder:
String achievementNames[] = getResources().getStringArray(R.array.achieveString);
String achievementDescriptions[] = getResources().getStringArray(R.array.achieveDescript);
However, when I run this, it gives me a null pointer exception on those lines of code. Is there a better way to pull string arrays or is there an error in my code?
Upvotes: 1
Views: 2005
Reputation:
You shouldn't call getResources()
unless onCreate()
callback has been triggered.
public class StackStackActivity extends Activity
{
String[] myArray = getResources().getStringArray(R.array.glenns); // This returns null
public StackStackActivity()
{
myArray = getResources().getStringArray(R.array.glenns); // This returns null also
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myArray = getResources().getStringArray(R.array.glenns); // Returns an array from resources
}
}
Upvotes: 2
Reputation: 83303
Make sure you pass a Context
object to wherever you call this method from (as my guess is that getResources()
is returning null
). With this Context
object, you can call,
context.getResources().getStringArray(...);
Upvotes: 0