findViewById returns null

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

findViewById returns null

Postby pseudorand » Mon Aug 16, 2010 2:30 am

Has anyone else had findViewById(R.id.xyz) return null? I have an app that does it. Not all the time, but with a specific TextView and EditView I just added, I always get null when I call findViewById for that object in the onCreate of my main activity. I've resorted to wrapping latter uses of that variable in if statements that call findViewById again if it's null. This works for the TextView in question, but the EditView always comes up null.

It seems like this should be impossible since I'm using an ID from R, which the SDK itself generated. It also seems like it just shouldn't compile is there were typos in my XML or Java (i.e. xyz wouldn't match anything generated in R.)

The first time this happened ran `ant uninstall; ant clean; ant install` and it went away, but it happened again with a new EditView and I can't findViewById to get a handle for this object. The object DOES appear on the screen just fine though.

I'm willing to deal with a few bugs on a new platform, but this one seems quite awful. Any hints on secret debugging or how to get Google's attention about such things.
pseudorand
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 26, 2010 6:13 am

Top

Re: findViewById returns null

Postby blundell » Mon Aug 16, 2010 9:41 am

Have you checked that your not importing android.R by accident?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: findViewById returns null

Postby pseudorand » Tue Aug 17, 2010 4:59 am

Yup. android.R isn't imported. I've tried on emulators running both 2.2 and 2.1, so it doesn't seem to be version related.
pseudorand
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 26, 2010 6:13 am

Re: findViewById returns null

Postby pseudorand » Tue Aug 17, 2010 5:26 am

I found the problem. I had just id instead of android:id in my XML. For some unfathomable reason, Android actually compiles the XML into R.java just as it normally would, but then fails to assign the id to the view object.

In google's defense, it does warn you when you compile, but it's easy to miss since it's in a different section than the normal compile warnings:
-package-resources:
[echo] Packaging resources
[aaptexec] Creating full resource package...
[null] ....\res\layout\main.xml:172: warning: found plain 'id' attribute; did you mean the new 'android:id' name?

In my defense, WTF google!?! What's the point of burdening us with a strongly typed language like Java and enforcing valid and well-formed XML if you can't even tell me about something like that. Bunch of douches.
pseudorand
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 26, 2010 6:13 am

Re: findViewById returns null

Postby blundell » Tue Aug 17, 2010 8:20 am

I have some vague recollection that plain ID was allowed back in Android 0.9 so they probably can't enforce it for compatibility bs, but nice1 for debugging it :-D
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: findViewById returns null

Postby pseudorand » Tue Aug 17, 2010 7:15 pm

Hey, if anyone wants to volunteer to write an android library that runs ruby scripts (maybe porting ruby/tk), I'd probably pay for that. I might even use (but not pay for) an python/android bridge.
pseudorand
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 26, 2010 6:13 am

Top

Re: findViewById returns null

Postby sjreed » Sat Feb 05, 2011 2:03 am

This post saved me!

This was my first attempt at creating a custom ListView. I had inadvertently entered 'android.id =' instead of 'android:id =' and it was obviously not working. The fact that someone else had entered this specification wrong gave me the idea to look in the xml file and find my problem!
sjreed
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Feb 05, 2011 1:58 am

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests