简体   繁体   中英

How can LinearLayout(linearLayout) be visible in my class?

I have two classes, the "XMLParser.java" extends the activity and imports the "ClassAsyncTask.java"(from my own library). Now Im having problem in my XMLParser, it says that the linearlayout is not visible, 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", this);
    aObj.execute();

    //THIS IS MY PROBLEM, "The field ClassAsyncTask.linearLayout is not visible"
    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(from my own Library) that was imported by XMLParser.java

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


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

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


@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++) {

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

    }
}

}

I suggest you don't access LinearLayout that way, and instead have setters and getters for it.

In your ClassAsyncTask.java

LinearLayout linearLayout;

public void setLinearLayout(LinearLayout linearLayout){
    this.linearLayout = linearLayout;
}


public LinearLayout getLinearLayout(){
    return linearLayout;
}

And then in your XMLParser.java , just do LinearLayout myLinearLayout = aobj.getLinearLayout() do the edits you want to the layout, then return it back to ClassAsyncTask via aobj.setLinearLayout(myLinearLayout) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM