7

I have been storing a global variable that's needed across Activities in my Android app by using a subclass of android.app.Application as explained by Soonil (in How to declare global variables in Android?).

The approach looks like this:

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();
    ...
    }
}

Up to this point, this approach has worked fine for accessing the global variable from any of my Activities. But today using the same approach, I got the following error:

Cannot make a static reference to the non-static method getApplicationContext()
from the type ContextWrapper

The key difference from before is that the new Activity is actually a Fragment (SherlockFragmentActivity, to be precise).

Any ideas why can't I access appState as I have before, and is there a good workaround?

Many thanks.


EDIT: Good catch, Matt B. It turns out the place I'm actually calling getApplicationContext() is inside another class. Here's the calling point:

public class MyActivity extends SherlockFragmentActivity {
    public static class AccountListFragment extends SherlockListFragment {
        MyApp appState = ((MyApp)getApplicationContext());
        ...
    }
    ...
}

Also, as noted below, the error went away when I changed the call to

MyApp appState = ((MyApp)getActivity().getApplicationContext());
Community
  • 1
  • 1
gcl1
  • 4,070
  • 11
  • 38
  • 55
  • Can we see the actual code you have in your activity that extends SherlockFragmentActivity? EDIT: I tried `Application appState = ((Application)getApplicationContext());` in my `SherlockFragmentActivity` and it compiles just fine. – Austyn Mahoney Jun 01 '12 at 17:43
  • I don't see where in this code sample you are attempting to make a static reference to that method. Are you sure you posted the correct line(s)? – matt b Jun 01 '12 at 17:47
  • can you then please mark the answer as correct? – vanleeuwenbram Jun 01 '12 at 18:20

1 Answers1

4
getActivity().getApplication() 

should work just fine.

You first need a reference to activity, then to application

The difference is that you are now calling this function from a Fragment (even though you named it "Activity") instead of an Activity

vanleeuwenbram
  • 1,289
  • 11
  • 19
  • According to his question he says it is a SherlockFragmentActivity. If he actually means SherlockFragment, then your answer may be correct. – Austyn Mahoney Jun 01 '12 at 17:47
  • "new Activity is actually a Fragment (SherlockFragmentActivity, to be precise)." I understand that he named his fragment "SherlockFragmentActivity" – vanleeuwenbram Jun 01 '12 at 17:48
  • Thank you, that seems to have cleared the error! To be clear though, do you know why do I need to add the getActivity() call in this case (from inside a SherlockFragmentActivity), but not previously (from inside a normal Activity)? – gcl1 Jun 01 '12 at 17:52
  • 1
    Well for one you are naming your `Fragment` an Activity, which is confusing. You shouldn't have the word Activity in the name of a class that is not have the base class of `Activity`. – Austyn Mahoney Jun 01 '12 at 17:54
  • The fragment code is from a github repository (http://code.google.com/p/sherlock-demo/source/browse/#git%2Fsrc%2Fcom%2Fexample%2Fandroid%2Fsherlockdemo). I also found the use of the term Activity a little confusing - but otherwise, the code seems like it will work :) . – gcl1 Jun 01 '12 at 18:10