Note that: (1) You don't need to use the Application class for this. I think it just makes things more complicated. I recommend just using a singleton.
(2) You need to be very very careful when doing this kind of stuff because you have nearly NO IDEA how long the data you put in the global will actually last. Once your app goes in the background (from the user pressing home, getting a phone call, etc) your process can be killed at any time, causing the global data to be lost. Often you will need to back that global data with something in persistent storage -- either through SharedPreferences, a database, a raw file, etc. On Thu, Apr 14, 2011 at 7:34 AM, Duskox <[email protected]> wrote: > Will do that, thanks! > > On Apr 14, 3:52 pm, Chris <[email protected]> wrote: > > I hadn't looked closely at your SET method but you're being bitten by > > the same problem. You're copying references, not data by doing it the > > way you currently are. Try: > > > > mGroups = new ArrayList<Group>(s); > > > > Which will create a NEW ArrayList object containing everything that > > your "s" arraylist contains. > > > > Chris > > > > On Apr 14, 9:43 am, Duskox <[email protected]> wrote: > > > > > > > > > > > > > > > > > Ok I tried now, bumped into antoher error but that is not related to > > > this problem. > > > > > But I noiced that now that I initialize or better say instantiate this > > > variable, array exists but is still empty, SET doesn't work. I guess > > > that was the problem all along, my setGlobalArrayVariable method is > > > not doing it's job :-/ > > > > > On Apr 14, 3:23 pm, Chris <[email protected]> wrote: > > > > > > This isn't C++. :) In Java, a variable to an object is a > > > > ReferenceType. It's kind of like a pointer in C++. So think of it > this > > > > way. If you created a pointer variable and passed it around without > > > > actually making it POINT to something first, what would happen? > You're > > > > passing around a NULL pointer. You need to create an object first > > > > which you're now doing in onCreate as you've said. Of course you > don't > > > > need to do this if your code is setup such that it ALWAYS calls SET > > > > before GET. It's fine to do a lazy initialization like that I guess. > > > > > > Chris > > > > > > On Apr 14, 8:57 am, Duskox <[email protected]> wrote: > > > > > > > Thanks that fixed it! > > > > > > > I created override of OnCreate in my application class and declared > a > > > > > new ArrayList. The only thing then puzzles me is why I had to do > it?? > > > > > I thought that it would automatically be filled with the object I > > > > > created in my init function that is inside of the initial Activity > > > > > class. I was thinking along the lines that this is like a pointer > > > > > variable and then I set it with passing a pointer to my object. I > > > > > guess it doesn't work that way :D > > > > > > > Thanks again!! > > > > > > > On Apr 14, 2:47 pm, Chris <[email protected]> wrote: > > > > > > > > You never instantiate mGroups in your Application class so unless > you > > > > > > SET it before GETing it, it's going to be null. > > > > > > > > Chris > > > > > > > > On Apr 14, 8:31 am, Duskox <[email protected]> wrote: > > > > > > > > > Hi all, > > > > > > > > > so I searched the net and found a nice way to make a global > variable > > > > > > > by creating a class that extends Application class. I did that. > But I > > > > > > > am having problems with it. So this is the situation: > > > > > > > > > public class Group { > > > > > > > public String mName; > > > > > > > public int mID; > > > > > > > public ArrayList<Items> mItems; > > > > > > > > > } > > > > > > > > > public class Item { > > > > > > > public String mName; > > > > > > > > > } > > > > > > > > > public class MyApplicationApp extends Application { > > > > > > > private ArrayList<Group> mGroups; > > > > > > > > > public ArrayList<Group> getGlobalArrayVariable() { > > > > > > > return mGroups; > > > > > > > } > > > > > > > > > public synchronized void > setGlobalArrayVariable(ArrayList<Group> s) { > > > > > > > mGroups = s; > > > > > > > } > > > > > > > > > } > > > > > > > > > So these are the classes I believe are cruical for this > question. Next > > > > > > > this is that in my Activity classes I set and get this > ArrayList > > > > > > > variable. So I set it in my initial Activity class that is > called when > > > > > > > the application is run. There I fill the array and just to be > sure I > > > > > > > read it with Log functionality after filling the array in the > > > > > > > MyApplicationApp class and everything is ok. > > > > > > > > > Then I try to read this from another Activity class where I go > to > > > > > > > after a menu choice. In that activity I successfully read the > > > > > > > application, meaning I get a handle to MyApplicationApp object > (I > > > > > > > guess). This is how I do it: > > > > > > > > > ... > > > > > > > private MyApplicationApp mMyApp; > > > > > > > private ArrayList<Group> mGroupsList; > > > > > > > > > protected void onCreate(Bundle savedInstanceState) { > > > > > > > super.onCreate(savedInstanceState); > > > > > > > > > mMyApp = > (MyApplicationApp)this.getApplication(); > > > > > > > if (mMyApp == null) Log.i(TAG,"mMyApp == > null"); > > > > > > > > > mGroupsList = > (ArrayList<Group>)mMyApp.getGlobalArrayVariable(); > > > > > > > if (mGroupsList == null) Log.i(TAG,"1. > mGroupsList == null"); > > > > > > > > > mGroupsList = > > > > > > > > ((MyApplicationApp)this.getApplication()).getGlobalArrayVariable(); > > > > > > > if (mGroupsList == null) Log.i(TAG,"2. > mGroupsList == null"); > > > > > > > ... > > > > > > > > > So mMyApp is not null and that is fine I guess, but mGroupsList > is > > > > > > > null and I presume that is way my app crashes after this last > log > > > > > > > entry. I guess it crashes because my variable is null?? Anyway > even if > > > > > > > the reason for crashing is other then this variable being null > I am > > > > > > > still puzzled why am I not able to get my global variable in > this > > > > > > > way?? > > > > > > > > > The reason for two different ways of getting something into > > > > > > > mGroupsList variable is because I was trying to see if I am > doing it > > > > > > > incorrectly but it is the same as far as I know... > > > > > > > > > Any ideas or pointers what should I do? > > > > > > > > > Thanks! > > > > > > > D. > > -- > 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 > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- 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

