Thursday, June 02, 2011

android development

I'm trying out android development for fun. I had added a subclass of Activity to my app, but found that the app was crashing because findViewById returned null when I tried to get a View in my Activity constructor:

myBlah = (Blah) findViewById(R.id.main);
myBlah.etc ... <== throws NullPointerException


I guess there are numerous ways this problem can occur, since I searched around on the web and found several different solutions, but none of them helped me. In my case, it turned out that I had called setContentView on the wrong xml file in my Activity constructor. For example: setContentView(R.layout.main); instead of setContentView(R.layout.view1);.

Once I fixed this, I was able to get beyond the problem and start debugging the next issue! I don't know what I'm doing yet, so I don't understand why I got a null return value here.

Labels: , ,

Tuesday, August 03, 2010

Aptana IE quirk

I'm working on a small application using Aptana Studio 2.0.5.

There's an html page which imports a Javascript file near the top. I decided to reorganize my Javascript file, and blam! the page stopped working in Aptana's "IE" view (the tab that appears next to the "Source" view). I checked out the page in Firefox 3.6.8, and it worked fine. Then I checked it outside of Aptana, in IE 8, and it worked fine there too!

Aptana's IE view is not a necessity but it is kind of handy. So I wanted it working again. Plus the fact that it wasn't working might mean there's an issue in other browsers.

By trimming the Javascript file and throwing in a couple of alerts, I figured out the problem: an extra comma. My original Javascript file ended like this:

manager = {
xMax : 12,
yMax : 12,
}

It was the trailing comma that killed the script in Aptana; once I removed that the page worked fine again. Do you want to play "spot the Java programmer"? In Java, trailing commas are allowed in arrays, and I sometimes use them when coding in Java.

So: are trailing commas forbidden in Javascript arrays? Way back in December of 2009 (practically prehistory), Amaxus said:
The most common IE problem we encounter is trailing commas in arrays. It doesn’t cause problems with Firefox, but it’s syntactically invalid and crashes IE.
That discussion was about IE 7, and apparently IE 8 is more lenient. However, the JSLint page says that trailing commas are a no-no in Javascript, also. I should probably run my Javascript through JSLint at some point.

Labels: , , ,