Android Day 4 & 5

Time to add some code…

I learned along time ago, when coding anything for the first time, start with something simple. So, I decided to code the “Clear” button, whose function is to clear the text from each text box on the screen.

While simultaneously reading through 4 books and 4 web sites I discovered there are at least 8 different ways to implement a listener. There is plenty of sample code around, but absolutely no explanation as to why author “A” does it this way, while author “B” does it another.

I finally stumbled upon a brilliant blog which explains all the different methods, and lists the pros and cons of each:
Implementing listeners in your Android application

With this limited information at hand I blindly proceeded to (1) Declare each widget on the screen, (2) Assign each widget based on their id, (3) set OnClickListeners for reach button.

Of course, on first run, there are errors galore, found I had to also import each widget. Sheesh, all this hand coding for such basic functionality.

Next run, I get a “failed to execute” error on the emulator. And so the fun begins. Scrutinizing each and every line of code for some silly typo (none). Taking advice from Eclipse to add get/set for each variable (nope). Recoding with various methods of implementing a listener (nada). It still errors out with a generic message containing no helpful troubleshooting information.

At this point, after scrutinizing the code until my eyes blur, I realize that there is some basic, fundamental thing that I am doing wrong.

The emulator has an error log, but I can’t find it. Tried searching for the log file, no luck. So, I finally ran it in Debug mode, which displays the error log in real time.

Sifting through the scores of messages, I found a type mismatch error
*sound of palm smacking forehead*
For some stupid reason I assigned RadioButton as RadioGroup. Fixed that, and, voila, it runs, YEAH.

But, of course, the Clear button doesn’t function. I had randomly picked an action that I thought might work .clearComposingText. Seemed like a logical choice, but it actually didn’t do anything. So I changed it to .setText(“”), and, holy heart failure batman, it worked.

Wow, I was able, over the course of a mere two days, to code the simplest, most insignificant function that this incredibly complex application will perform!

And I am beginning to develop a deep mistrust of Java.

Comments are closed.