英文:
Cannot resolve "array" in R.array.items (android studio)
问题
以下是翻译好的内容:
我知道很多类似这样的问题已经存在,但我已经浏览了它们,没有找到答案。
由于某种原因,关键字“array”被用红色下划线标出,我得到了错误:无法解析“array”。我已经构建了项目,检查了我的xml文件,但我就是找不出问题出在哪里。
package com.example.listapp;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView myListView;
String[] fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
myListView = (ListView)findViewById(R.id.my_list_view);
fruits = res.getStringArray(R.array.items);
}
}
此外,这是我的strings.xml文件(位于res/values/strings.xml):
<resources>
<string name="app_name">List App</string>
<string-array name="items">
<item>peach</item>
<item>apple</item>
<item>banana</item>
</string-array>
<string-array name="prices">
<item>$1.49</item>
<item>$0.99</item>
<item>$0.89</item>
</string-array>
<string-array name="descriptions">
<item>Fresh peaches from Georgia</item>
<item>Fresh apples from Ohio</item>
<item>Fresh bananas from California</item>
</string-array>
</resources>
有错误的那一行是:
fruits = res.getStringArray(R.array.items);
英文:
I know many questions like this one already exist, but I looked through them and couldn't find an answer.
For some reason the keyword "array" is underlined in red, and I am getting the error: Cannot resolve "array". I have built the project, checked my xml file, but I just cannot figure out what is wrong.
package com.example.listapp;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView myListView;
String[] fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
myListView = (ListView)findViewById(R.id.my_list_view);
fruits = res.getStringArray(R.array.items);
}
}
Also here is my strings.xml file (under res/values/strings.xml):
<resources>
<string name="app_name">List App</string>
<string-array name="items">
<item>peach</item>
<item>apple</item>
<item>banana</item>
</string-array>
<string-array name="prices">
<item>$1.49</item>
<item>$0.99</item>
<item>$0.89</item>
</string-array>
<string-array name="descriptions">
<item>Fresh peaches from Georgia</item>
<item>Fresh apples from Ohio</item>
<item>Fresh bananas from California</item>
</string-array>
</resources>
The line with the error is:
> fruits = res.getStringArray(R.array.items);
答案1
得分: 4
将res
替换为getResources()
。
尝试如下:
final String[] values = getResources().getStringArray(R.array.items);
清理并重新构建您的项目,或者无效崩溃/重启。
英文:
Replace res
to getResources()
Try like this:
final String[] values = getResources().getStringArray(R.array.items);
Clean
and Re-Build
your projct or Invalid Crashes/ Restart
答案2
得分: 1
<array name="select_city">
<item>Surat</item>
<item>Ahmedabad</item>
<item>Vadodara</item>
<item>Anand</item>
<item>Amreli</item>
<item>Rajkot</item>
</array>
然后可以像这样使用:
String[] abc = getResources().getStringArray(R.array.select_city);
英文:
<array name="select_city">
<item>Surat</item>
<item>Ahmedabad</item>
<item>Vadodara</item>
<item>Anand</item>
<item>Amreli</item>
<item>Rajkot</item>
</array>
then use like this
String [] abc=getResources().getStringArray(R.array.select_city);
</details>
# 答案3
**得分**: 1
```java
String[] values = getResources().getStringArray(R.array.items);
如果仍然出现错误,请尝试以下操作:
**Build->Clean Project**
或者
**File->Invalid Crashes/Restart**
我认为这将解决你的问题。
```
<details>
<summary>英文:</summary>
String[] values = getResources().getStringArray(R.array.items);
This will be work, If u still getting error, please
**Build->Clean Project** or
**File->Invalid Crashes/ Restart**
I think it will solve your problem
</details>
# 答案4
**得分**: 0
以下是翻译好的内容:
错误的主要原因是在值文件夹中没有 `array.xml` 文件,但你在 `string.xml` 中创建了 `string-array`。
> 解决方法:
在值文件夹中创建 `array.xml` 文件,并将所有的 `string-array` 复制并粘贴到 `array.xml` 中。
[![在这里输入图片描述][1]][1]
[1]: https://i.stack.imgur.com/qFfDx.jpg
<details>
<summary>英文:</summary>
The main reason behind this error is you have no `array.xml` file in values folder your are created `string-array` in `string.xml`
> Solution:
Create the `array.xml` file in values folder and copy & past all `string-array` to `array.xml`
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/qFfDx.jpg
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论