You are right, my example is a bit confusing. lib in OtherActivity
should be MainActivity.lib.

Regards,

Michael A.

On Aug 6, 5:11 pm, DanH <[email protected]> wrote:
> I'm a little curious.  A static should only be reset when a class (not
> object) goes away .  A class can only go away when its class loader
> and all classes referencing it go away.  This is independent of when
> individual objects get created (except that you can't create an object
> unless it's class is there, and a class can't go away while there are
> existing objects).
>
> I suppose it's possible that Dalvik violates these rules somehow, but
> if so it needs to be well-documented.
>
> Your example is confusing, however.  Where is the "lib" in
> OtherActivity defined, and how is it initialized?
>
> On Aug 6, 6:48 am, "Michael A." <[email protected]> wrote:
>
> > I am seeing a lot of NullPointerException in my code at the moment
> > from public static variables.
>
> > I use these variables to store, e.g., my image library (used in
> > Activities throughout my code). All of them are initialized in the
> > OnCreate of my apps Base activity, and I check that they are
> > initialized in OnResume just to be certain (and initialize them if
> > they have somehow become nulled).
>
> > // Main APP Activity
> > MainActivity {
> > public static GlobalObject lib;
>
> > void OnCreate() {
> >   if (lib == null)
> >     lib = new GlobalObject();
>
> > }
>
> > void OnResume() {
> >   if (lib == null)
> >     lib = new GlobalObject();
>
> > }
> > }
>
> > // Some other activity instantiated later
> > OtherActivity {
> > void onCreate() {
> >   lib.doSomething();  // This line will sometimes fail with a
> > NullPointerException
>
> > }
> > }
>
> > I am quite unable to recreate the situation in which this
> > NullPointerException occurs. As I understand the activity lifecycle,
> > it should not be possible for lib to be null when OtherActivity is
> > created (if OtherActivity is in memory, MainActivity's static
> > variables should also be in memory and if the app is killed, it should
> > start over from MainActivity?), but that is the error I seem to be
> > getting.
>
> > Suggestions, anyone? I suppose I am probably overlooking something
> > simple, but I just can't see it.
>
> > Regards,
>
> > Michael A.

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

Reply via email to