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 1053431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:15:07+00:00 2026-05-16T17:15:07+00:00

In my application I need data which is accessible for a few activities. I’ve

  • 0

In my application I need data which is accessible for a few activities. I’ve read that a good solution is to use Application class for this. So I use it like this:

public class MyApplication extends Application {

  private String str;

  public String getStr(){
    return str;
  }
  public void setStr(String s){
    str = s;
  }
}

and I can access this variable from activity like this:

MyApplication appState = ((MyApplication)getApplicationContext());
String str = appState.getStr();

It’s ok, but I also have xml parser class:

public class MyXMLHandler extends DefaultHandler {

and if I try to do the same here

    MyApplication app = ((MyApplication)getApplicationContext());
    String str = app.getStr();

I’m getting The method getApplicationContext() is undefined for the type MyXMLHandler

How can I access my variable?

  • 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-05-16T17:15:07+00:00Added an answer on May 16, 2026 at 5:15 pm

    Well, usually an XML parser class should be independent of any special context. That means a developer should be able to use it no matter whether he’s developing an application or a service or library or whathever.

    The XML parser class should not make any assumptions as to the context it is being used in and where it gets parameters from (you’d restrict your parser to function only if it has access to an Application instance). The parser should not fetch its parameters, the parameters should be set by the caller.

    You wouldn’t want your XML parser class to show messages to the user, either, would you? Right: “What does an XML parser have to do with user interfaces?” Instead, you’d throw exceptions and make sure they are handled properly, for example depending on whether there’s a user interface or not (logging).

    So what you’d do is pass the parameters you need when constructing an instance of your XML parser. But you do not pass your application instance as a parameter (think again of dependencies), but you pass the necessary parameters from your application class.

    In your example above:

    MyApplication app = ((MyApplication)getApplicationContext());
    MyXmlHandler handler = new MyXmlHandler(app.getStr());
    

    You should really make sure to keep “tool stuff” separate from anything that would prevent you from using it universally. What would happen if you wanted to use your XML Parser class in another project where your parameter is not provided by the application context but some other class?

    I’m sure that you can have a week-long discussion about object-oriented design and how things should be done – but that’s basically how I’d do it…

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

Sidebar

Related Questions

I need to develop an application which stores data in a SQL Server 2005
I need the Select Data Source dialog added to my application so that the
In certain areas in my application I need data from several tables in the
To implement data access code in our application we need some framework to wrap
I am working on an ASP.NET MVC application where I need to export data
I need to maintain a legacy VB.NET web application. Data typing is, I would
I'm facing the need to enter ragged data in a WinForms application. Some columns
I need to make a web application and I want to use MVC. However,
For an automated setup build that generates the setup for an application which uses
In an application I need to execute other programs with another user's credentials. Currently

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.