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

  • SEARCH
  • Home
  • 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 8493229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:56:50+00:00 2026-06-10T22:56:50+00:00

I have two classes, this one is XMLParser.java extends the activity and execute the

  • 0

I have two classes, this one is XMLParser.java extends the activity and execute the ClassAsynTask. Now Im having problem in my ClassAsyncTask, in onPostExecute part where you can find the getApplicationContext(), How can I fix this error? thanks

public class XMLParser extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ClassAsyncTask aObj = new ClassAsyncTask(
            "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
    aObj.execute();

    /* layout to display the view */
    aObj.linearLayout = new LinearLayout(this);
    aObj.linearLayout.setOrientation(1);

    /* Set the ContentView to layout for display */
    this.setContentView(aObj.linearLayout);

}

}

This is the ClassAsyncTask.java

public class ClassAsyncTask extends AsyncTask<String, Void, String> {


String targetURL;
TextView name[], website[], category[];
LinearLayout linearLayout;

public ClassAsyncTask(String site){
    targetURL = site;
}


@Override
protected String doInBackground(String... urls) {

    try {

        /* Handling XML */
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        /* Send URL to parse XML Tags */
        URL sourceUrl = new URL(targetURL);

        /* Create handler to handle XML Tags ( extends DefaultHandler ) */
        MyXMLHandler myXMLHandler = new MyXMLHandler();
        xr.setContentHandler(myXMLHandler);
        xr.parse(new InputSource(sourceUrl.openStream()));

    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }
    return null;
}

/* Return-value from doInBackground */
protected void onPostExecute(String result) {

    /* Get result from MyXMLHandler SitlesList Object */
    SiteList sitesList = MyXMLHandler.sitesList;

    /* Assign TextView array length by arrayList size */
    name = new TextView[sitesList.getName().size()];
    website = new TextView[sitesList.getName().size()];
    category = new TextView[sitesList.getName().size()];

    int h = sitesList.getName().size();
    /* Set the result text in TextView and add it to layout */
    for (int i = 0; i < h; i++) {

        //This is where Im getting the error.
        name[i]= new TextView(getApplicationContext());
        name[i].setText("Name = " + sitesList.getName().get(i));

        website[i]= new TextView(getApplicationContext());
        website[i].setText("Website = " + sitesList.getWebsite().get(i));

        category[i]= new TextView(getApplicationContext());
        category[i].setText("Website Category = " + sitesList.getCategory().get(i));

        linearLayout.addView(name[i]);
        linearLayout.addView(website[i]);
        linearLayout.addView(category[i]);

    }
}

}
  • 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-10T22:56:52+00:00Added an answer on June 10, 2026 at 10:56 pm

    The easiest way is to pass the context into your AsyncTask constructor and then use it when required:

    public class ClassAsyncTask extends AsyncTask {
        Context context;
        ...
    
        public ClassAsyncTask(String site, Context ctx){
            targetURL = site;
            context = ctx;
        }
    
        ...
    
    }
    

    And create the task as:

    ClassAsyncTask aObj = new ClassAsyncTask(
            "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml", this);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one activity, off this I have two classes. When my app starts
I have a one-to-many relationship between two classes for this situation. I have a
having this two classes I always have a doubt of which is the best
I have two classes [applicable to this question]. The first, XYAxes2 , extends FrameworkElement
Note: This is a homework assignment. I have two classes, one inherits from the
I have two classes, one is a standard activity , the other a service
I have defined two classes in javascript as follows. function ParentClass(){ this.one = function(){
Suppose I have two classes. In the first one I declare this in Class1.h
I have two classes, one of them is a configuration class. Inside of this
Supposed I have two classes like this: public class Parent { public string Id

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.