0

我有两个类,一个是 XMLParser.java 扩展活动并执行 ClassAsynTask。现在我的 ClassAsyncTask 有问题,在 onPostExecute 部分可以找到 getApplicationContext(),我该如何解决这个错误?谢谢

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);

}

}

这是 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]);

    }
}

}
4

3 回答 3

1

最简单的方法是将上下文传递给您的AsyncTask构造函数,然后在需要时使用它:

public class ClassAsyncTask extends AsyncTask {
    Context context;
    ...

    public ClassAsyncTask(String site, Context ctx){
        targetURL = site;
        context = ctx;
    }

    ...

}

并将任务创建为:

ClassAsyncTask aObj = new ClassAsyncTask(
        "http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml", this);
于 2012-09-06T10:37:15.630 回答
1

您可以创建一个构造函数,然后您可以使用此上下文。私有上下文上下文;

ClassAsyncTask(Context context){
 this.context=context;

}

于 2012-09-06T10:44:41.730 回答
0

在你的 onCreate 方法中放这个:

Context context = getApplicationContext();

使用上下文变量代替getApplicationContext()inonPostExecute

于 2012-09-06T10:39:31.200 回答