[英]Reading XML Strings from Java
With something that I thought was so simple, I'm surprised to be getting such a strange error...有了一些我认为很简单的事情,我很惊讶会遇到这样一个奇怪的错误......
In my program, I have a layout with 5 buttons on it.在我的程序中,我有一个带有 5 个按钮的布局。 When you press a button, it launches the phone's dialer with the number pre-loaded into it.
当您按下一个按钮时,它会启动手机的拨号器,其中预装了号码。 I've had no problem with this before, but then I tried moving the phone numbers to strings in an XML file that I put in the /res/values folder called 'phone.xml'.
我之前对此没有任何问题,但后来我尝试将电话号码移动到 XML 文件中的字符串,该文件我放在名为“phone.xml”的 /res/values 文件夹中。 Here's a portion of my code for the Java file:
这是我的 Java 文件代码的一部分:
public void launchDialer(String number){
String numberToDial = "tel:"+number;
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(numberToDial)));
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.police_button1:
launchDialer(Integer.toString(R.string.police1_phone));
break;
case R.id.police_button2:
launchDialer("" + R.string.police2_phone);
break;
case R.id.police_button3:
launchDialer("" + R.string.police3_phone);
break;
case R.id.police_button4:
launchDialer("" + R.string.police4_phone);
break;
case R.id.police_button5:
launchDialer("" + R.string.police5_phone);
break;
}
}
And here's my phone.xml file:这是我的手机。xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="police1_phone">"555-555-5555"</string>
<string name="police2_phone">1-800-555-5555</string>
<string name="police3_phone">555-555-5555</string>
<string name="police4_phone">555-555-5555</string>
<string name="police5_phone">555-555-5555</string>
</resources>
As you can see in the Java file, I tried something different for police1_phone and police2_phone, but neither of them worked.正如您在 Java 文件中看到的那样,我为police1_phone 和police2_phone 尝试了不同的方法,但它们都不起作用。 You can also see I tried putting quotes around 1 of the phone numbers in the xml file, but it still didn't work.
您还可以看到我尝试在 xml 文件中的 1 个电话号码周围加上引号,但它仍然不起作用。 My output has always been some random 7 digit number that wasn't even close to the phone number I wanted it to print.
我的 output 一直是一些随机的 7 位数字,甚至与我希望它打印的电话号码都不接近。 This Java code worked:
此 Java 代码有效:
launchDialer("555-555-5555");
But, I need it to read from an XML file.但是,我需要它从 XML 文件中读取。 Any ideas?
有任何想法吗?
Maybe I don't understand something, but getString(R.string.police1_phone)
must work.也许我不明白,但
getString(R.string.police1_phone)
必须工作。
If you want they are ready to use parsers like如果您希望他们准备好使用解析器,例如
SAXXParser (which i allways use) http://www.javaworld.com/javaworld/jw-05-2002/jw-0517-sax.html SAXXParser(我总是使用) http://www.javaworld.com/javaworld/jw-05-2002/jw-0517-sax.html
JAVAXML: http://www.mkyong.com/tutorials/java-xml-tutorials/ JAVAXML: http://www.mkyong.com/tutorials/java-xml-tutorials/
you could write your own little parser if you want.如果你愿意,你可以编写自己的小解析器。
If you want to use R.string.xxx
, then you need to put your string values in a file named strings.xml
in your /res/values/
folder.如果要使用
R.string.xxx
,则需要将字符串值放入/res/values/
文件夹中名为strings.xml
的文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.