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