I'm trying to share two ArrayLists across the various activities in my application, using the scheme explained here: How to declare global variables in Android?.
Here's my application subclass:
public class GlobalVars extends Application{ ArrayList<Player> players = new ArrayList<Player>(); ArrayList<String> playerNames = new ArrayList<String>(); public ArrayList<Player> getPlayers(){ return players; } public ArrayList<String> getPlayerNames(){ return playerNames; } public void setPlayers(ArrayList<Player> p){ players = p; } public void setPlayerNames(ArrayList<String> pn){ playerNames = pn; } }
And used the code:
GlobalVars gv = (GlobalVars)getApplicationContext(); players = gv.getPlayers(); playerNames = gv.getPlayerNames();
To access these variables. The first line there where I define gv throws a classcastexception. Anyone know why?
Here's the code I added to the manifest:
<application android:name="com.myname.GlobalVars" android:icon="@drawable/icon" android:label="@string/app_name"></application>
edit:for clarification, here is my entire manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myname.bpstattracker" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".BPStatTracker" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BPSTAdd"></activity> <activity android:name=".OneOrThree"></activity> <activity android:name=".SixOrTen"></activity> </application> <application android:name="com.myname.GlobalVars" android:icon="@drawable/icon" android:label="@string/app_name"> </application> </manifest>
To elaborate on Peter K's answer, you do not need to create a second <application>
section for your derived Application class. You simply need to "rename" your existing <application>
section by amending it with the android:name
tag. I also want to point out that the fully qualified classname is required(as you've done correctly...I found this out the hard way).
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myname.bpstattracker" android:versionCode="1" android:versionName="1.0"> <application android:name="com.myname.GlobalVars" android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".BPStatTracker" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BPSTAdd"></activity> <activity android:name=".OneOrThree"></activity> <activity android:name=".SixOrTen"></activity> </application> </manifest>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With