2

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. 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'. Here's a portion of my code for the Java file:

    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 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. You can also see I tried putting quotes around 1 of the phone numbers in the xml file, but it still didn't work. My output has always been some random 7 digit number that wasn't even close to the phone number I wanted it to print. This Java code worked:

launchDialer("555-555-5555");

But, I need it to read from an XML file. Any ideas?

Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • 2
    "neither of them worked" doesn't give us much information. What happened? Did you get an exception? What was the value of `R.string.police2_phone` at execution time? Basically there's a bunch of diagnostics missing here... – Jon Skeet Aug 17 '11 at 12:45
  • As I mentioned, I gave me a random 7-digit number in the dialer. The number it gave me was '213-096-8576', which was far from the actual number, which was '978-937-xxxx'. No matter how I worded the Java/XML file as described above, they all gave me random 7-digit numbers. – Mxyk Aug 17 '11 at 12:49

3 Answers3

8

Maybe I don't understand something, but getString(R.string.police1_phone) must work.

Michael
  • 53,859
  • 22
  • 133
  • 139
  • I don't why any of my other attempts don't work, but this works. Thank you! – Mxyk Aug 17 '11 at 13:05
  • 2
    Because you were using the id of the resource(generated in the R class), not the value itself. – Adinia Aug 17 '11 at 13:22
  • Yes, @Adinia is right. Id is just an integer which you can use to load a resource. Ids are generated by the Resource Compiler so they're just random numbers for you. – Michael Aug 17 '11 at 14:08
0

If you want they are ready to use parsers like

GrandMarquis
  • 1,913
  • 1
  • 18
  • 30
0

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.

Bitmap
  • 12,402
  • 16
  • 64
  • 91
user895758
  • 11
  • 1
  • 7
  • I would, but my strings folder is full of a bunch of other strings (this project is relatively huge; this phone portion is a very small section of the project). The reason I want it in a separate local file is because eventually I want to move on to parsing the phone numbers from an online xml file, if that makes sense. – Mxyk Aug 17 '11 at 13:02