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! While you're at it, check out some resources Treehouse students have shared here.
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 trialLouis Sankey
22,595 PointsDeclaring a constant String and setting it in Android
I am having trouble with declaring a constant string variable name URL and setting it to the value "teamtreehouse.com" The last line of code in the onCreate method is what I tried, among other things.
''' Java
package com.example;
import android.os.Bundle; import android.view.View; import java.net.MalformedURLException; import android.util.Log;
public class MainListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
public static final String URL = "teamtreehouse.com;
}
}
''' Any help would be appreciated.
I also don't know what I'm doing wrong in my formatting on these posts. I tried '''java... ''', so any help would be appreciated there are well.
3 Answers
Louis Sankey
22,595 PointsNever mind. I added the code in the wrong place.
cbcbcb
16,560 PointsHey Louis Sankey, I saw you already got it all sorted out but I thought I might include some information you might find of value later on down the road..possibly very soon or maybe not.
It is recommended that you declare all String constants in your /res/values/strings.xml file in your project like this:
<string name="URL">teamtreehouse.com</string>
One of the benefits to doing this is that it will allow you to not only organize your code better but to provide internationalization of your content by providing different values for different languages based upon the user's locale setting on their device.
For example, some web sites provide a sub domain for different locales ( Examles: en.gravatar.com, fr.gravatar.com , etc, etc)
Hope that was helpful even if it may have seemed pointless just for your one value
A tip is to highlight your constant and right-click, then refactor, then extract constant.
Good luck to you Louis
Louis Sankey
22,595 PointsThanks Christopher, I appreciate the tip. The lesson that I was watching actually touched on this exact same point. It's always good to have it reinforced by hearing it in a different way, however. In addition, I appreciate the camaraderie.
Louis Sankey
22,595 PointsLouis Sankey
22,595 PointsNever mind, I got it figured out. I was adding the code in the wrong place.