Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:21:46+00:00 2026-06-04T19:21:46+00:00

I have been storing a global variable that’s needed across Activities in my Android

  • 0

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());
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:21:46+00:00Added an answer on June 4, 2026 at 7:21 pm
    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have lots of global read only strings (around 100) that I am using
We have been storing our staging database on the production database server with the
So far during my site development I have been storing all my jQuery functions
I'm working on a game, and we have been storing our level information in
Today I have been experimenting with SQL binary objects. I started by storing an
So I have been learning assembly and came to the topic of stack, storing
I have been passed a date string that looks like this: Thu%20Mar%2011%202010%2015%3A09%3A11%20GMT%2B0000%20(BST) I want
I have been tasked with securing the connection string in an classic ASP application
I'm creating an Android application (Android 2.3.3) that has a header, a footer and
I have been using php/mysql for a while now, I want to sanitize all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.