简体   繁体   English

Android [字符串数组]无法解析或不是字段

[英]Android [string array] cannot be resolved or is not a field

I'm getting an error about a string array (located in strings.xml ) not being able to be resolved. 我收到有关无法解析的字符串数组(位于strings.xml )的错误。

I've done everything the internet says: 我已经做了互联网上所说的一切:

  1. Restarted Eclipse 重新启动Eclipse
  2. Deleted my R.java file inside the gen folder 删除了gen文件夹中的R.java文件
  3. My values folder is still inside the res folder, not moved 我的values文件夹仍然在res文件夹中,没有移动
  4. None of my XML files have erros 我的XML文件都没有错误
  5. Ran Project -> Clean Project -> Clean
  6. I don't have android.R imported in my class. 我的课堂上没有导入android.R
  7. Checked the R.java class, it indeed doesn't have an app_categories property (my array's name) 检查了R.java类,它确实没有app_categories属性(我的数组的名称)

The relevant part of the XML: XML的相关部分:

<string-array name="app_categories">
    <item >Cat 1</item>
    <item >Cat 2</item>
    <item >Cat 3</item>
    <item >Cat 4</item>
    <item >Cat 5</item>
    <item >Cat 6</item>
    <item >Cat 7</item>
    <item >Cat 8</item>
    <item >Cat 9</item>
</string-array>

The Java code: Java代码:

String[] categoryNames = getResources().getStringArray(R.string.app_categories);

All my other strings are visible. 我所有其他字符串都可见。 I have another array defined just like this one, and that one isn't visible either. 我有另一个与此定义的数组,并且该数组也不可见。

Exact error below: 确切的错误如下:

app_categories cannot be resolved or is not a field [class_name].java   [path]  line 45 Java Problem

Just try out this way : 只需尝试这种方式:

  getResources().getString(R.array.app_categories_list); 

Instead of R.string use R.array you will get your arraylist. 代替R.string使用R.array您将获得您的arraylist。

Don't put your string arrays in strings.xml You must create a new file called arrays.xml and place them there. 不要将字符串数组放在strings.xml中,您必须创建一个名为arrays.xml的新文件并将其放在此处。 And when calling you need R.arrays.myarray, not R.strings.MyArray 当调用时,您需要R.arrays.myarray,而不是R.strings.MyArray

清理项目后,尝试通过从命令行运行adb kill-server和adb start-server来重新启动adb服务器

a solution may be to create the R manually: 一个解决方案可能是手动创建R:

just hover over the error in the activity file where the problem was and then manually create it, which will cause R to generate the string array you created in strings.xml 只需将鼠标悬停在问题所在的活动文件中的错误上,然后手动创建它,这将导致R生成您在strings.xml中创建的字符串数组

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

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