Posts

Showing posts from 2010

Examining Android database tables using an adb shell and sqlite3

Image
In this post we see how easy it is to use an ADB shell and sqlite3 to examine the contents of a SQLite database on an Android device. I will be using the Android Notepad tutorial as an example. To start build and run the Notepad tutorial using the emulator. I am using the Notepad V4 version I created by upgrading the NotepadV3solution, as this removes a couple of annoying bugs. Instructions on how to build this are given in the previous post. While the application is running on the emulator, create a number of notes with known contents. I created 4 notes with titles of 1, 2, 3 and 4 and bodies of “Body 1”, “Body 2” etc… Now on a windows command line open a shell on the emulator using the ADB command Adb –e shell You will get a # prompt, showing that you are now running a linux shell on the emulator. Android stores SQLite databases in /data/data/{application package name}/databases. So we need to navigate to this directory. I do this in a few steps. (1) cd data/data (2) ls (3) cd my.and...

Using Log.v, LogCat and Filters when debugging an Android Application

Image
When debugging an Android Application one of the most useful tools is Log.v. This is a means of sending debug messages to the log file, which can be read in real-time with LogCat. I will illustrate this using a real bug in the Android Notepad tutorial. If you have been following this blog you will now have a version of Notepad (NotepadV4) that makes it easy to add edit and delete Notes. If you run it in the emulator, chose the DDMS perspective in Java and select the emulator then you will see LogCat showing the log messages from Android. Try editing a number of notes within the Notepad application. After editing 4 the following error messages appeared in LogCat. I have maxmised the LogCat Window as the error messages are quite long. The crucial error messages are: 06-07 14:54:31.530: ERROR/Database(221): java.lang.IllegalStateException: /data/data/my.android.demo.notepad4/databases/data SQLiteDatabase created and never closed and 06-07 14:54:31.530: ERROR/Database(221): at my.andr...

Extending the Android Notepad tutorial (Notepad v4)

Image
In order to develop my understanding of Android and the Notepad tutorial I decided to extend it, and correct some bugs. I first created a copy of the Notepad3solution tutorial directory named NotepadV4 and created an Eclipse Project called NotepadV4 from this source code. The detailed steps to accomplish this within Eclipse were given in my previous post. Once you have the project up on running on the emulator it will look like this. The first change is to correct the application name shown in the ListView. Changing the application name is easy using the XML editor provided within the ADT Eclipse plugin. Simply update the app_name string within strings.xml using the editors resources tab as shown below. The next change is to improve the No Notes! message. Due to a minor bug within Notepad3solution, this is currently defined within notes_list.xml in the following: textview id="@+id/android:empty" layout_width="wrap_content" layout_height="wrap_content" text...

The Android Notepad tutorial (exercise 3)

Image
At the end of exercise 2 we found Notepadv2 crashes if the back button is pressed while the activity is in the foreground. Notepadv3 adds a full activity lifecycle to the NoteEdit class to solve this. This exercise is well documented in http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html Rather than importing the Notepadv3 code supplied as a new project, I started a new project based on the final code I had for Notepadv2. Outside of Eclipse I copied the Notepadv2 folder to create a Notepadv3 folder in the Eclipse workspace. Then in Eclipse I selected File -> New -> Android Project. I then selected ‘create project from existing source’, and filled in the project and activity names. If you are following this, click on Finish. Right click on the new project in the package explorer window and select close unrelated projects. Now we need to create a notepadv3 activity from the notepadv2 activity. Double click on the Notepadv2.java class in the package explorer w...

The Android Notepad tutorial (exercise 2)

Image
If you are continuing to follow the android Notepad tutorial, you will now be carrying out exercise 2. This adds a context menu and has a separate NoteEdit activity invoked to create or edit Notes. This exercise is well documented in http://developer.android.com/resources/tutorials/notepad/notepad-ex2.html I again found this exercise very straight-forward. Here are a few tips I found helpful 1. By this stage we have several android projects within the Eclipse IDE, including another called Notepad. I found it useful to close all other projects so I was sure I was only editing classes within the current exercise. To do this enter the Java perspective, find the current project within the package explorer window and choose close unrelated projects. 2. In step 2 I got an error OnCreateContextMenu must override a supertype. Pressing Control-Shift-O to import any missing packages resolved this. 3.You may, like me, have wondered about the purpose of the line @Override that occurs before many o...

The Android Notepad tutorial (exercise 1)

The next Android tutorial is the Notepad tutorial documented at http://developer.android.com/resources/tutorials/notepad/index.html I found exercise 1 pretty straight forward. As I noted a couple of posts ago one of the most difficult aspects is getting the XML layout and resources files right. Here are a few tips that will make it easier. 1. When writing or amending your XML files, Eclipse will not validate them until you save them. 2. Errors in the XML layout files will be indicated by red circles in the left hand column of the XML source window. Hovering over the circle with the mouse will give a description of the error. 3. Right clicking in the window and selecting validate will cause the file to be saved and validated. However I found that this process reported there to be no errors or warnings even if there were errors in the XML. 4. After resolving several problems I had an error Unparsed aapt errors – check the console for output. Android packaging problem Cleaning the projec...

Debugging Android NullPointer Exceptions

Image
A lot of the errors that you will get while writing programs in Java to run on Android will be NullPointer exceptions. They are easy to debug with the tools that Eclipse and ADT give you. If you resume the suspended Hello World Program we left in the last post you will get a NullPointer Exception and be returned to the debug perspective of Eclipse. In the debug window, you will see the call stack starts with Thread [ main] (Suspended (exception RuntimeException)) – giving you a clear indication of the cause of the runtime error. If you click on the top stack frame icon(horizonal blue bars) in the variables window you will see a grey circle labelled e with value Nullpointer Exception – again giving you a clear indication of the error. The Java source code window is blank, because the tab is showing code relevant to the stack frame which is inside the Dalvik virtual machine and not displayed. If you click on the hello android tab in this window you will see that line 13 is still highli...

The Android Hello World tutorial (part 2)

Image
If you are continuing to follow the android tutorial, you will now be creating a new hello world program that defines its string inside the strings.xml file rather than inside the helloAndroid class. In the tutorial you write XML directly. I found that it was easy to make errors. Luckily the Android Debug Tools (ADT) Eclipse plug-in means that you often don’t have to write XML. For example if you double click on the strings.xml file in the res/layout directory, Eclipse will open a window on the xml file. If you click on the resources tab at the bottom of the window you can add, edit, delete and reorder strings without having to worry about matching your start and end tags etc. If you do the same to the main.xml file in the res/layout directory Eclipse will show you the resulting string drawn on the device screen. The tutorial ends by deliberately creating a null pointer exception and showing how to create a breakpoint. If you run the program in debug mode with a breakpoint set on line ...

The Android Hello World tutorial

Image
In time honoured fashion the first android program you write will be a hello world program. This is very straight forward and well documented in http://developer.android.com/guide/tutorials/hello-world.html Here are a few tips. 1. When building your program you will receive the following warning about each of the XML files in the build. No grammar constraints (DTD or XML schema) detected for the document The XML files used to build android applications don’t use DTD or a schema, so I would recommend turning this warning off within Eclipse. Choose preferences from the Eclipse Windows top level drop down menu. The warning is turned off as in the screenshot below. 2. When you run your hello world application the tutorial says you will see a screen like this. 3. You will not, you will see this. It is nothing to worry about, it is just that android will take a significant time to boot up and run your application in the emulator. I am using an IBM T60 with a regularly degfragmented hard dis...

Getting started with Android

Image
The first step in getting started with Android is to download your development environment. This involves: 1. Downloading and installing Eclipse. 2. Downloading and installing the Android Software Development Kit (SDK) 3. Installing the Android Debug Tools (ADT) plugin for Eclipse 4. Adding the target platforms and other components. This is documented clearly on the http://developer.android.com/sdk/index.html page. I am developing on an IBM T60 laptop running Windows XP SP3, so I downloaded Eclipse IDE for Java Developers (92 MB), the Android SDK, the Android ADT and all the target platforms. When I downloaded the Android SDK, unzipped it and ran setup.exe I got an error message: failed to fetch URL https://d-ssl.google.com/android/repository/repository.xml reason: HTTPS SSL error. You might want to force download through HTTP in the settings. You do this in the settings menu of the setup program as shown below. The final thing I would point out here is the instruction: To install the ...

Who is Steve Roome and why would you read his blog?

My name is Stephen Roome, and I am a Senior Managing Consultant at IBM Global Services in the UK where I specialise in complex system integration projects. I have been in the IT industry for over 30 years. I first learnt to program in Fortran writing characters onto squared paper and translating the results onto punched cards that were then fed into a mainframe. Over the years I have learnt and used other languages including BCPL, PL/M, C and C++. As I got more senior in the IT industry I lost the opportunity to write code and instead led the work of others. At IBM I have been an advocate for Linux and open source software. I have promoted the advantages of open source software both internally and to numerous customers. The projects I have led over the last few years have tended to be written in Java using Eclipse and run on Linux. However until recently I had never used Java, Eclipse or even logged onto a Linux system. As I entered my mid fifty's I decided that I needed to get b...