开发者

Extending Application to share variables globally

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>

    开发者_JAVA百科    <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>


To be clear,

<application
    android:allowBackup="true"
    android:icon="@drawable/icon_app"
    android:label="@string/app_title"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

Is my existing manifest. To declare another application within my existing manifest, and or app all together, i would simply add:

android:name="com.XXXXXX.data.DataStore"

So the end result will be:

<application
    android:name="com.XXXXXX.data.DataStore" *<-- class that extends Application and has global variables with exact package and class name*
    android:allowBackup="true"
    android:icon="@drawable/icon_app"
    android:label="@string/app_title"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >


You should call getApplication() in your Activity or Service:

GlobalVars gv = (GlobalVars)getApplication();

Edit:

You have <application> defined twice in your manifest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜