Is onBackPressed called from a different thread perhaps? I wouldn't have thought so, but stranger things have happened. And if that's the case, how can I ensure that my variable is initialized. I cannot construct it on the decleration as the object depends on the intent passed in to the Activity.
My Activity can take a couple of seconds to display, and if I press the back button quickly one of two things happen on my Nexus One: it either doesn't register at all or that the activity flickers on and off and I am back to the original Activity. In no case did it crash in my testing. But as I said, I got some other errors like this for a member that should have been initialized in onResume. On Jul 24, 4:33 pm, Doug <[email protected]> wrote: > Sounds like it might be possible that onBackPressed can get called > before onCreate in some circumstances where the device is being slow? > > If that is at all a possibility, maybe you should check for null in > onBackPressed! :-) > > Doug > > On Jul 23, 4:00 pm, Zsolt Vasvari <[email protected]> wrote: > > > > > > > > > I am getting some bugreports of NPE's which I cannot explain. > > > These happen rarely. Here's the must baffling (in simplified code): > > > class MyActivity extends Activity > > { > > Object myObject; > > > protected void onCreate(Bundle state) > > { > > myObject = new Object(); > > } > > > public void onBackPressed() > > { > > myObject.myMethod(); > > } > > > } > > > Then the user gets a NPE on myObject when it tries to call myMethod in > > obBackPressed() > > > There are no other myObject assignemnet in my code -- once it's > > inialized, it's never touched. So the only way I can see that NPE > > happenining is if onBackPressed() is called before onCreate(). > > > I've had some other reports where the NPE happened after a screen > > rotate, but it involved a member variable, which should have been also > > initalized in onCreate(). > > > Since these reports are rare with only 1 report per error, I suspect a > > race conditiion. > > > Would love to hear people's ideas. (And please don't tell me to chech > > myObject != null before accessing) -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

