title: Can't make static reference to non-static method ( Android getApplicationContext() )
body: <p>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 <a href="https://stackoverflow.com/questions/708012/android-how-to-declare-global-variables">How to declare global variables in Android?</a>).</p>
  
  <p>The approach looks like this:</p>
  
  <pre><code>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();
      ...
      }
  }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>Cannot make a static reference to the non-static method getApplicationContext()
  from the type ContextWrapper
  </code></pre>
  
  <p>The key difference from before is that the new Activity is actually a Fragment (SherlockFragmentActivity, to be precise).</p>
  
  <p>Any ideas why can't I access appState as I have before, and is there a good workaround?</p>
  
  <p>Many thanks.</p>
  
  <hr>
  
  <p>EDIT: Good catch, Matt B.  It turns out the place I'm actually calling getApplicationContext() is inside another class.  Here's the calling point:</p>
  
  <pre><code>public class MyActivity extends SherlockFragmentActivity {
      public static class AccountListFragment extends SherlockListFragment {
          MyApp appState = ((MyApp)getApplicationContext());
          ...
      }
      ...
  }
  </code></pre>
  
  <p>Also, as noted below, the error went away when I changed the call to</p>
  
  <pre><code>MyApp appState = ((MyApp)getActivity().getApplicationContext());
  </code></pre>