【问题标题】:Android - referencing string array using another string with getIdentifier() and getStringArrayAndroid - 使用带有 getIdentifier() 和 getStringArray 的另一个字符串引用字符串数组
【发布时间】:2011-09-27 06:35:18
【问题描述】:

根据用户决定使用哪一个,我想要访问各种字符串数组。我不想使用 SQLite DB,因为我对 Android/Java 很陌生,而且我一直在努力寻找示例,所以我猜这是一种相当糟糕的方法,但都是一样的......

如果我在 xml 文件中有这个:

    <string-array name="bob">
<item>1</item>
<item>4</item>
<item>7</item>
<item>11</item>
</string-array>

在 Java 文件中:

String name = "bob";

为什么以下不起作用?每次启动都会崩溃。

int holderint = getResources().getIdentifier("name", "array",
                this.getPackageName());
String[] items = getResources().getStringArray(holderint);

【问题讨论】:

    标签: android arrays string


    【解决方案1】:

    这条线不应该是这样的吗?

    int holderint = getResources().getIdentifier(name, "array",
                    this.getPackageName()); // You had used "name"
    String[] items = getResources().getStringArray(holderint);
    

    【讨论】:

    • 你说得对,拉胡尔。非常感谢!我使用的网站显然是错误的(或者我实施得不好)。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 2021-07-19
    • 2011-06-12
    • 1970-01-01
    • 2010-10-24
    • 2013-08-07
    • 1970-01-01
    相关资源
    最近更新 更多