Android Noob – Custom View Wont Draw – Here’s Why

August 8th, 2009

I defined a custom SurfaceView and included an onDraw() method.

It wasn’t getting called – a mystery.

I added a background color to its declaration in main.xml and that fixed it.

Grrr.

Android Noob – Performance requirements and “bad” Java

August 2nd, 2009

THere is a section in the SDK docs on designing for performance. It has lots of suggestions, and some of them run contrary to “good Java” practice. Hence, if you do good Java practice, you want to look at these.

For example, they discourage getters/setters, preferring public access between classes. This is a big Object Oriented and Java No-No. But it’s the right thing to do where you need it.

Android Noob – Duh – Where the JVM Log (and exceptions) lurk

August 2nd, 2009

Learning to work with Android, I guess it helps to play around with all the tools before doing too much serious development.

I had been wondering where to find stack traces when my code blew out with uncaught exceptions, or failed to start.

Well, I need look no farther. One of the functions of the adb utility is just that:

adb.exe logcat

It does the trick and shows lots of other interesting traces.

And, you can write to it yourself using the Log class in the SDK.

Android – SDK/Eclipse Screwup causes Runtime Exception

August 1st, 2009

I created an android project in eclipse that had a custom MySurfaceView object that was a subclass of SurfaceView and was declared in the layout. I refactored this project several times.

When I tried to run it, it blew up immediately.

I used adb.exe logcat to look at the logs and it was having trouble “inflating” MySurfaceView – couldn’t find the class.

After wasting lots of time, I suspected that the project wasn’t being built correctly. I created a new ASurfaceView class and put all the MySurfaceView stuff in it so it was identical except for the name. I then deleted MySurfaceView.java.

The problems was fixed.

Android – Noob – How to Get a Click on View

July 12th, 2009

Here is how to arrange to get a click on a view that doesn’t have an onClick() method to override.

  this.setOnClickListener(
        new OnClickListener() {
            @Override
            public void onClick(View v) {
                 _mainThread.setCenter();
            }
        }
  );

Android – Noob – Simple Sprite Graphics

July 12th, 2009

So I wanted to have an object that floated around over a fixed background. This is called a “sprite.”

Examining the documentation didn’t show the word “sprite” anywhere. However, it turns out that the Lunar Lander example included with the SDK does sprite graphics (even uses the word) to animate the landing craft.

It’s pretty simple – just draw the object on your view’s graphics object.

Android – Noob – Application Blows Up Before First Statement

July 12th, 2009

One of the most frustrating debug problems is when the application blows up before it gets to the first debug statement.

Here are a couple of hints:

  1. Using the debugger, look at the stack trace. It may have clues in it (or it may not tell you much)
  2. Examing the metadata XML files and be sure all references to Java classes are exactly right – package name spelled right, etc

Android Programming – Noob – Debug Logging

July 11th, 2009

So I wanted to log my accelerometer averager without slowing it down significantly…

Android provides two things that together make this easy (although I haven’t figured out how to get a snapshot as opposed to a scrolling log):

  1. A logging class – in my case, I used:

    android.util.Log.d(”ACC”, “dX:” + dampedX + ” LX:” + lastX + ” buf[" + avgOldest + "]=” + avgBufX[avgOldest]);

  2. A debug interface for the device or emulator that does lots of cool things, including showing the log in real time. This is called ddms and is found in the SDK tools directory. It is a batch file that fires up a GUI. See this write-up.

;

Android Programming – Noob – Debug Fails on Actual Device

July 11th, 2009

Android debugging can be done either with the emulator, or directly on the hardware device via the USB connection.

I had an app which was debugging fine on the emulator, but on the phone, just ran without hitting breakpoints.

It turns out that one needs to add the attribute android:debuggable=”true” to the application XML element.

It would be helpful if the emulator were to raise a question when this element is missing, since this represents a failure to accurately emulate the device.

Android Programming – Noob – How to get Periodic Events

July 11th, 2009

I have a graphics application that needs to get periodic events in order to update a sensor data filter.

It turns out the way this is normally done is to create an independent thread with a main loop which calls whatever needs a “tick” periodically.

This is used in the Lunar Lander demo that comes with the SDK.

PROBLEM: this is not very accurate real time. It would be better to have an interval timer that does not depend on the vagaries of running a loop with “sleep” in it. android.os.CountDownTimer may do this, but it does want to stop eventually.

Experimenting…

update…

android.os.CountDownTime does the trick