Index

Android apps - Getting the code OK

As every programmer know, it is not enough to code a program or app, you have to get the code 'clean' (clearing up any stupid formatting problems) and you have to run the program and debug it (get it working). This section deals with getting the code OK.

I've found AndroidStudio quite helpful with this. (Compared to JavaScript - oh Lumey!)

The important place to look is the top right-hand corner of the listings. For example, go to your "MainActivity". In the top right of the listing, there is either a green tick, an orange square or a red exclamation mark. (A black eye seems to mean that it hasn't decided yet!) Green tick is good - pat yourself on the back. Red exclamation mark is bad. You need to clean them up before you can run the app. Orange box are warnings, which you need to check. I prefer to always have green tick.

By the way, if you can't see this green tick / orange box / red exclamation mark, then see if there is an irritating message in about the same place telling you that there's a new version of AndroidStudio available, and get rid of the message (grr!!!) The green tick etc. should be underneath.

AndroidStudo also tells you what's wrong, and where. For red exclamation mark, there is a short red line somewhere below it. Point the mouse at it, and it will tell you the error message (and I've found them helpful). Click on it, and the listing will scroll to the right place and position the cursor there. It stays on "MainActivity" though, so if the problem is that you've forgotten to define something in "strings.xml" or "content_main.xml", it merely tells you that it can't resolve the reference. The orange square works similarly to the red exclamation mark, except the problems aren't as bad, of course.

Apart from obvious things like mispelling stuff and forgetting to define things, or defining them twice by mistake, or getting format wrong, my two main errors (since I come from JavaScript) are forgetting the semi-colon at the end of statements, and getting variable types wrong, such as handing over integers to method when they should be doubles, and so on. No dount, you will come up with your own favourite errors, depending on your past coding experience.

I also have grievous problems with Java's carefree attitude to integer and real arithmetic . However, this won't show up at this stage, of course. It tend to just give wrong (and, to me, inexplicable) results from a calculation.... See Numbers if you have no idea what I mean.

One thing that helps to produce clean code is AndroidStudio's helpful suggestions when you start typing a statement. It's well worth taking up these suggestions, as they stop a lot of typing errors (OK - I can't spell! Right? It's genetic...) Also, if you keep an eye on things, you can see that AndroidStudio is nudging you about things you've defined and not used, or used and not defined. Useful.

One last point - when checking for the green arrow, remember to check the "content_main.xml" and "strings.xml" green arrows as well! If "content_main.xml" is showing a screen rather than a listing, click on Text (rather than Design) at the bottom on the listing.


© Jo Edkins 2016 - Index to Android app index