我是 Android 新手,在代码中使用资源中的字符串变量时遇到问题。我尝试了在互联网和 Android API 指南上找到的一些解决方案,但它们在这种特定情况下不起作用,也可能是我没有正确使用它们。
更具体地说,我有一个主/详细信息流程 Activity ,我想使用资源字符串作为项目名称以实现多语言目的,但我在恢复实际字符串时遇到问题。 我得到的错误是:
Cannot resolve method 'getString()'
这是我基于 android studio 虚拟文件的代码
public class Categories {
public static List<CatName> ITEMS = new ArrayList<CatName>();
static {
String temp = getString(R.string.cat_n1);
addItem(new CatName("1", temp);
}
private static void addItem(CatName item) {
ITEMS.add(item);
}
public static class CatName {
public String id;
public String name;
public FieldCat(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return name;
}
}}
最佳答案
您需要指定资源。试试这个,
getResources().getString(R.string.cat_n1);
getString(int resId)
:从应用程序包的默认字符串表中返回本地化字符串。
getResources().getString(int id)
:返回与特定资源 ID 关联的字符串值。它将删除任何样式文本信息。
关于java - 使用列表中资源中的字符串。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30600585/