Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在对象中使用资源字符串?-安卓_Android_String - Fatal编程技术网

Android 如何在对象中使用资源字符串?-安卓

Android 如何在对象中使用资源字符串?-安卓,android,string,Android,String,我在安卓系统中有这样的代码 public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"}; public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"}; 我想把这个代码变成这样 public static final String[] URLS = new

我在安卓系统中有这样的代码

public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"};
public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"};
我想把这个代码变成这样

public static final String[] URLS = new String[]{"https://www.url1.com/","https://www.url2.com"};
public static final String[] URLS = new String[]{"@strings/url_1","@strings/url_2"};
我尝试了太多的代码,但没有成功。我能怎么办?
非常感谢。

@string资源由R.java文件中的int引用保存。所以,你不能直接使用它们。然而,还有另一种方法可以做到这一点

可以在arrays.xml文件中使用字符串数组。那就像

<string-array name="urls">
    <item>https://www.url1.com/</item>
    <item>https://www.url2.com/</item>
    <item>https://www.url3.com/</item>
</string-array>
您需要使用从android资源获取字符串。请尝试以下操作

Resources res=context.getResources();
公共静态最终字符串[]url=新字符串[]{res.getString(R.String.url_1),res.getString(R.String.url_2)};