简体   繁体   English

Coudnt在main-activity中调用数组

[英]Coudnt call the array in main-activity

I've created 2 arrays in my string.xml. 我在string.xml中创建了2个数组。 But when i call the strings in my MainActivity.java it says "array could not be retrieved or not a field" 但是,当我在MainActivity.java中调用字符串时,它说“无法检索数组或没有字段”

can anyone help me to fix this. 谁能帮我解决这个问题。 thank you 谢谢

strings.xml 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 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. 1]确保您导入了正确的R类。 it is a common phenomenon to import android.R instead of your project's R. 导入android.R而不是项目的R是很常见的现象。

2] after checking 1], clean and rebuild your application 2]检查1]之后,清理并重建您的应用程序

Also put fetching array coding part either in onCreate() or use your MainActivity context of that class with getResources() else you probably get NullPointerException . 还要将获取数组编码部分放在onCreate()或者将您的类的MainActivity context与getResources()一起使用,否则您可能会得到NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM