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