1) Don't subclass Activity for classes that do not display anything.
Activity (and any subclasses) is designed to be a visual class.

2) Create your own class with a constructor that takes a Context instance
and use that instance to get the shared preferences object.

I have code like this in one of my Receiver classes (which is also
non-visual):
SharedPreferences prefMgr =
PreferenceManager.getDefaultSharedPreferences(context);

----------------------------------------------------------------------
There are only 10 types of people in the world...
Those who know binary and those who don't.
----------------------------------------------------------------------


On Sat, Dec 12, 2009 at 4:28 PM, DrLaban <[email protected]> wrote:

> Hi!
>
> I'm a beginner at Android but have coded a bit of Java before.
>
> I'm trying to figure out how I'm supposed to instantiate and use a
> class that does not contain any visual elements, but holds a lot of
> functions for processing data.
> My main activity, on either a button click or by pressing the back-
> button, shall call functions in another class, which is void of visual
> elements, that takes care of data processing.
> I like to keep my classes logically separated and this non-visual
> class can and will be used by other classes later throughout the
> application.
>
> I have hit a few speed bumps on the road and I now need some help. I
> have two issues;
>
> Preface:
> I have two classes. One called "textfields" that contains a view of a
> bunch of textfields and a button. This is an Activity-class.
> I also have a non-visual class "logic" that contains all the functions
> I need for logic handling of data and values. This is, in my mind, a
> non-Activity based class (though I might be wrong).
> These classes use getSharedPreferences to store and fetch values from
> time to time. I am also going to extend my application to make real
> good use of the stored values, but I need to start somewhere. :)
>
> 1:
> Now, for instance, in my Activity "textfields" I instantiate my visual-
> less class called "logic" at the onCreate() event.
> When the onPause() event fires in "textfields", I call a function in
> the "logic"-class.
> The "logic" class reads values stored in getSharedPreferences. Here, I
> get a NullPointerException when executing getSharedPreferences within
> the "logic"-class. I'm extending the "logic"-class with Activity, in
> order to be able to use the getSharedPreferences-function at all. Why
> am I getting a NullPointerException and how should I solve the
> problem?
>
> 2:
> I have no constructor in the "logic"-class as it has been stated on a
> few occasions that we instead should use onCreate(), onResume(), etc.
> I can't figure out when any of these events fire when I'm not using a
> class containing an Activity. I have tried to override and use super
> to chain the events, but nothing I do calls either onCreate, onResume
> or whichever event when I from "textfields" instantiate "logic".
> "Logic" doesn't seem to be running onCreate() or onResume() as a class
> 'usually' does. When I want to use a non-visual class, what is the
> better way of doing this? I also need access to getSharedPreferences.
>
> Thank you for your time, I'm appreciating any help I can get.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-beginners%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" 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-beginners?hl=en

Reply via email to