[英]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: 我已经做了互联网上所说的一切:
gen
folder gen
文件夹中的R.java文件 values
folder is still inside the res
folder, not moved values
文件夹仍然在res
文件夹中,没有移动 Project -> Clean
Project -> Clean
android.R
imported in my class. android.R
。 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.