Pages

8.25.2013

Beginning Android programming

This summer I decided to engage in some extracurricular coding; I bought an e-book on Android programming and began typing away.   I soon realized that I really had no idea  what it was that I was doing.  It's pretty difficult to actually understand the code you're writing if all you're doing is typing the sample code segments from the book.

I decided that I needed to venture away from the book and write an app of my own.  Additionally, I wanted to gain some experience with version control software, so I created a Github account to hold my project.  And thus Better Shopping List was born.

Over the past 10 days, I've brought a very basic list keeping app into existence with minimal copy/pasting from the internet.  As the project has progressed, I've been forced to consult the Android API many times in order to solved my problems.


My most recent problem was an app crash that would occur under the following cirsumstances:
User is editing an existing item



User presses Android "Recents" button

Doing so causes the app to crash and produces the following stack trace:

 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.coffeestrike.bettershoppinglist.Item)

Eventually, I was able to trace the issue back to my own "Item" class,   a model class for holding the information about an item on the shopping list.   It turns out the the way I was storing the description of the item, as a CharSequence that is, was not serializable.  It turns out that when the dialog is open for editing,  the EditText field uses SpannableStringBuilder to represent the editable text. When onStop() method was called, the activity attempted to serialize the SpannableStringBuilder. In fact, SpannableStringBuilder implements the CharSequence interface, but is not itself serializable.

The fix was incredibly simple.  I changed the member variable mDescription from type CharSequence to type String, and refactored the setDescription() method.  No more stupid app crash.


The point is, that reading a book and writing sample code won't make you into a programmer.  Learning how to use someone else's API and writing your own code on the other hand definitely will.


No comments: