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: , , ,

Friday, May 18, 2007

file date comparison in perl

This is a little sample code that shows how to compare the last-modified time for one file with that of another in Perl:

#!/usr/bin/perl
use strict;
use warnings;

my $ofile = "/home/wereldmuis/oldfile";
my $nfile = "/home/wereldmuis/newfile";
open(FO, $ofile);
open(FN, $nfile);

my @info = stat FO;
my $oftime = $info[8]; # file modification time in epoch seconds

@info = stat FN;
my $nftime = $info[8];

my $current = time(); # current time in epoch seconds, just an example

if ($nftime < $oftime){
 print "$nfile is older than $ofile\n";
} else {
 print "$ofile is older than $nfile\n";
}

Output is "/home/wereldmuis/oldfile is older than /home/wereldmuis/newfile".

Labels: ,

Tuesday, March 13, 2007

How to stop perl from exiting

Here's how to stop perl from exiting: use eval (a subset of the rule RTFM)! Here's some test code:

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use XML::Simple;

my $loc = "/tmp/bad.xml";
my $xmldata = eval { XMLin($loc) };
if ($@) {
print "problem with xmldata in $loc, skip it\n";
} else {
print Dumper($xmldata);
}

$loc = "/tmp/good.xml";
$xmldata = eval { XMLin($loc) };
if ($@) {
print "problem with xmldata in $loc, skip it\n";
} else {
print Dumper($xmldata);
}
This is bad.xml:
<metadata>
<file name="hello.file">
Simon & Garfunkel
</file>
</metadata>
And this is good.xml:
<metadata>
<file name="hello.file">
Simon &amp; Garfunkel
</file>
</metadata>

And finally here's the output:

wereldmuis@hostname:~$ perl testing.pl
problem with xmldata in /tmp/bad.xml, skip it
$VAR1 = {
'file' => {
'content' => '
Simon & Garfunkel
',
'name' => 'hello.file'
}
};

Labels: , ,