Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

David Drees
David Drees
9,219 Points

Code Challenge: All About @string Resources, Task 5 [SOLVED]

Hello Forum,

I'm failing to get past the final code challenge titled "All About @string Resources." Here is the error:

Bummer! Compilation Error!

And here is my code

Resources resources = getResources();
phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);
//String[] phoneNumberLabels = { "-home", "-work", "-mobile" };

Button saveButton = (Button) findViewById(R.id.saveButton);
String label = getString(R.string.saveLabel);
saveButton.setText(label);

The prompt is:

Update the 'phoneNumbers' variable to get its values from strings.xml instead of those already present in the code. First you need a Resources object using the 'getResources()' method and then call 'getStringArray()' from the 'resources' variable.

I'm not really sure what I'm doing wrong. A nudge in the right direction would be appreciated!

Thanks, David

5 Answers

David Kaneshiro Jr.
David Kaneshiro Jr.
29,247 Points

David,

You still need to include 'String[]' in your phoneNumberLabels declaration.

David Drees
David Drees
9,219 Points

Thanks! I will try it out now.

this worked but it only worked upon removing "protect" from my declaration. In other words, I had initially declared my variable phoneNumbers as so:

protected String[] phoneNumbers;

and changed it to:

String[] phoneNumbers;

I was wondering why including protect in the declaration yields a compilation error. Thanks!

David Kaneshiro Jr.
David Kaneshiro Jr.
29,247 Points

You have to remember that this is only a code challenge so the answer it is looking for is usually going to be code that it is expecting. Even though adding 'protected' in front of the array declaration may work in XCode the challenge isn't expecting the 'protected' key word to be there. That's probably why there was a compilation error.

The String[] was missing in the second line. The full answer is:

Resources resources = getResources();
String[] phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);

Button saveButton = (Button) findViewById(R.id.saveButton);
String label = getString(R.string.saveLabel);
saveButton.setText(label);
Rajesh Mule
Rajesh Mule
2,125 Points

Resources resources = getResources(); phoneNumberLabels = resources.getStringArray(R.array.phoneNumbers);

This does not work.

Lara Drobig
Lara Drobig
5,980 Points

Hey,

your need to add the whole getRescources() method.So it's getResources().getStringArray(R.array.phoneNumbers)

Your answer is a little incomplete. Did you mean that the top 2 lines are

Resources resources = getResources();
phoneNumberLabels = getResources().getStringArray(R.array.phoneNumbers)

because that did not work