RSS阅读器,,,在范围内无法访问Main2类型的封闭实例

时间:2014-04-22 15:47:57

标签: android eclipse rss android-arrayadapter

我想制作RSS阅读器,但我有这个问题“没有封闭的类型Main2实例可以在范围内访问”onpostexecute for arrayadapter无法定义为“this”类我搜索该问题但不能找到解决方案>>> pleaaaase任何人都可以帮助:(

public class Main2 extends ListActivity {
Button grow, price, disease;
Intent go, pr, dis;
List headlines;
List links;

public Main2() {
    // TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    // Binding data
    // ArrayAdapter adapter = new ArrayAdapter(this,
    // android.R.layout.simple_list_item_1, headlines);
    //
    // setListAdapter(adapter);
    new GetRSS().execute();

    grow = (Button) findViewById(R.id.bt_plants);
    price = (Button) findViewById(R.id.bt_prices);
    disease = (Button) findViewById(R.id.bt_diseases);
    grow.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            go = new Intent(getApplicationContext(), Plantgrow.class);
            startActivity(go);
        }
    });
    price.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            pr = new Intent(getApplicationContext(), Price.class);
            startActivity(pr);

        }
    });
    disease.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dis = new Intent(getApplicationContext(), Diseases.class);
            startActivity(dis);
        }
    });

}

protected void onListItemClick(ListView l, View v, int position, long id) {
    Uri uri = Uri.parse((String) links.get(position));
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main2, menu);
    return true;
}

}

class GetRSS extends AsyncTask<String, String, List> {
List Aheadlines, Alinks;

protected List doInBackground(String... args) {
    // List headlines, links;
    Aheadlines = new ArrayList();
    Alinks = new ArrayList();

    try {
        URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

        // We will get the XML from an input stream
        xpp.setInput(getInputStream(url), "UTF_8");

        /*
         * We will parse the XML content looking for the "<title>" tag which
         * appears inside the "<item>" tag. However, we should take in
         * consideration that the rss feed name also is enclosed in a
         * "<title>" tag. As we know, every feed begins with these lines:
         * "<channel><title>Feed_Name</title>...." so we should skip the
         * "<title>" tag which is a child of "<channel>" tag, and take in
         * consideration only "<title>" tag which is a child of "<item>"
         * 
         * In order to achieve this, we will make use of a boolean variable.
         */
        boolean insideItem = false;

        // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("item")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("title")) {
                    if (insideItem)
                        Aheadlines.add(xpp.nextText()); // extract the
                                                        // headline
                } else if (xpp.getName().equalsIgnoreCase("link")) {
                    if (insideItem)
                        Alinks.add(xpp.nextText()); // extract the link of
                                                    // article
                }
            } else if (eventType == XmlPullParser.END_TAG
                    && xpp.getName().equalsIgnoreCase("item")) {
                insideItem = false;
            }

            eventType = xpp.next(); // move to next element
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Aheadlines;
}

protected void onPostExecute(List feed) {
    // TODO: check this.exception
    // TODO: do something with the feed
    super.onPostExecute(feed);

    // ListView itcItems = (ListView) findViewById(android.R.id.list);

    // Create a list adapter

    ArrayAdapter adapter = new ArrayAdapter(Main2.this,
            android.R.layout.simple_list_item_1, Aheadlines);
    // Set list adapter for the ListView
    Main2.setListAdapter(adapter);

}

//
public InputStream getInputStream(URL url) {
    try {
        return url.openConnection().getInputStream();
    } catch (IOException e) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为您的AsyncTask GetRSS不是Main2的内部类。

你必须在一个内部类中创建(所以基本上你只需要在声明你的GetRSS类之前删除一个{。然后在你文件的末尾添加一个}

这样,你的GetRSS类将是一个内部类,你的Main2将在它的范围内。