Activity.findViewById returning null sporadically

Posted by adstro on Stack Overflow See other posts from Stack Overflow or by adstro
Published on 2011-01-12T03:53:15Z Indexed on 2011/01/12 5:54 UTC
Read the original article Hit count: 145

Filed under:

From the crash logs that I am getting from the Android market, I can see that some of my users are getting Force Closes caused by NullPointerExceptions when my code tries to access views that are in my application. In one example, my activity makes a call to findViewById() in onCreate() after I call setContentView(). I get an NPE when I try to access the view after the call to findViewById() (still in onCreate). What has me really scratching my head is that this does not happen all of the time (in fact most of the time the code acts as I would expect), but enough to have me concerned.

I could add code to always check for null and avoid the NPE, but I would like to understand what could be causing the sporadic behavior.

Does anyone know what could be causing this?

© Stack Overflow or respective owner

Related posts about android