1. Are you ready for the Galaxy S20? Here is everything we know so far!

Global variables in android app

Discussion in 'Android Development' started by eljainc, Nov 4, 2010.

  1. eljainc

    eljainc Lurker
    Thread Starter

    Hello,

    I'm trying to implement a global property to variables. From another activity, I'd like to access values that are in another activity. From what I've seen through Googling, it seems that adding a class like the following will resolve this issue:

    class MyApp extends Application {

    private String myState;

    public String getState(){
    return myState;
    }
    public void setState(String s){
    myState = s;
    }
    }

    class Blah extends Activity {

    @Override
    public void onCreate(Bundle b){
    ...
    MyApp appState = ((MyApp)getApplicationContext());
    String state = appState.getState();
    ...
    }
    }

    After doing this, I'm getting application errors immediately when the application starts execution on the emulator. What would cause this behavior?

    Is there a better way to have global variables in use throughout an android application?

    Thanks
    Mike
     



    1. Download the Forums for Android™ app!


      Download

       
  2. mblenton

    mblenton Lurker

    Did you register the Application in the AndroidManifest.xml?

    <application android:label="@string/app_name"
    android:name="YourApplication">

    etc...
     
Loading...
Similar Threads - Global variables android
  1. Gianni Mitchell
    Replies:
    2
    Views:
    654
  2. ironass
    Replies:
    1
    Views:
    3,250
  3. ironass
    Replies:
    1
    Views:
    3,519
  4. ironass
    Replies:
    1
    Views:
    3,381
  5. ironass
    Replies:
    3
    Views:
    2,227
  6. ironass
    Replies:
    4
    Views:
    4,963
  7. ironass
    Replies:
    4
    Views:
    5,072
  8. ironass
    Replies:
    2
    Views:
    2,584
  9. ironass
    Replies:
    2
    Views:
    2,498
  10. ironass
    Replies:
    2
    Views:
    2,386

Share This Page

Loading...