208 CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View
I look for error messages that feature line numbers that I know are reasonable for the amount of
code that I have written, such as the line of code annotated as java:21, shown highlighted in light
blue in Figure 6-37.
Since I haven’t written thousands of lines of code, I know the other error messages referenced are
not in my code, although those errors are certainly caused by my code. What I want to see is what
line of my code is causing the error, so I look at Line:21, which is shown highlighted in the screen
shot, and references the latest line of code you just wrote, which attempts to pass an integer data
value to a method expecting a text data value.
What we need to do to correct this error is add to our Java programming statement’s structure in
such a way as to convert the numeric value into a text value “on its way in” to the method call. In
pseudo-code, this would be:
TextViewObject.setTextMethodCall( ConvertNumberToTextMethodCall(GalaxyObjectNumericDataValue) );
There is indeed a method in Java, in the String class that we have already used to create String
objects, called the .valueOf( ) method, which will turn a numeric value into a String text value, by
using the following format:
String stringVariableName = String.valueOf(numeric variable here);
In the construct we are using, we can nest this String.valueOf( ) method call inside of our existing
Java structure:
solarData.setText( String.valueOf(milkyWay.galaxySolarSystems) );
Java is pretty cool in the way that you can nest and chain (using dot notation) constructs together
to get dense code constructs; that is, to accomplish a lot of what I call programming “moves” using
a single line of code. We will take a look at Java code chain constructs later on in the book as we get
into more and more complex topics and code structures. This may be an Absolute Beginners book,
but Android topics in it are inherently advanced. My apologies if you thought Android programming
was ever going to be some sort of cakewalk! Once you have the solarData line of code working,
replicate it for the other five numeric data values, as shown in Figure 6-38.