I've created 2 arrays in my string.xml. But when i call the strings in my MainActivity.java it says "array could not be retrieved or not a field"
can anyone help me to fix this. thank you
strings.xml
<string-array name="place_type">
<item>hospitals</item>
<item>health</item>
<item>doctor</item>
<item>pharmacy</item>
</string-array>
<string-array name="place_type_name">
<item>HOSPITALS</item>
<item>HEALTH</item>
<item>DOCTOR</item>
<item>PHARMACY</item>
</string-array>
MainActivity.java
// Array of place types
mPlaceType = getResources().getStringArray(R.array.place_type);
// Array of place type names
mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);
Follow below steps :
1] make sure you have imported the right R class. it is a common phenomenon to import android.R instead of your project's R.
2] after checking 1], clean and rebuild your application
Also put fetching array coding part either in onCreate()
or use your MainActivity
context
of that class with getResources() else you probably get NullPointerException
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.