我试图添加到RSS Feed阅读器(ListActivity)ActionBar - (http://www.edumobile.org/android/android-tutorial/rssreader-in-android-development/)
我用Fragment来做这件事。
现在我收到错误,我不知道如何解决它
No enclosing instance of the type News is accessible in scope
错误在:
rssadaptor = new RSSListAdaptor(News.this, R.layout.newslist,itemlist);
//Here
progress = ProgressDialog.show(
News.this, null, "Loading RSS Feeds...");
//Here
LayoutInflater vi = (LayoutInflater)News.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Here
以下是活动的完整代码:
package com.rss.test.actionbar;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class News extends ActionBarActivity {
private static ArrayList<NewsItem> itemlist = null;
private static MyFragment.RSSListAdaptor rssadaptor = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionbar = getSupportActionBar();
setContentView(R.layout.news);
actionbar.setDisplayShowCustomEnabled(true);
itemlist = new ArrayList<NewsItem>();
MyFragment fragment = new MyFragment();
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, fragment).commit();
fragment.new RetrieveRSSFeeds().execute();
}
public static class MyFragment extends ListFragment {
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
NewsItem data = itemlist.get(position);
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(data.link));
startActivity(intent);
}
private void retrieveRSSFeed(String urlToRssFeed,ArrayList<NewsItem> list)
{
try
{
URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RSSParser theRssHandler = new RSSParser(list);
xmlreader.setContentHandler(theRssHandler);
InputSource is = new InputSource(url.openStream());
xmlreader.parse(is);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private class RetrieveRSSFeeds extends AsyncTask<Void, Void, Void>
{
private ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
retrieveRSSFeed("/*RSS Url*/",itemlist);
rssadaptor = new RSSListAdaptor(News.this, R.layout.newslist,itemlist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(
News.this, null, "Loading RSS Feeds...");
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
ListView l = (ListView)getActivity().findViewById(R.id.list);
l.setAdapter(rssadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
private class RSSListAdaptor extends ArrayAdapter<NewsItem>{
private List<NewsItem> objects = null;
public RSSListAdaptor(Context context, int textviewid, List<NewsItem> objects) {
super(context, textviewid, objects);
this.objects = objects;
}
@Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public NewsItem getItem(int position) {
return ((null != objects) ? objects.get(position) : null);
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if(null == view)
{
LayoutInflater vi = (LayoutInflater)News.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.newslist, null);
}
NewsItem data = objects.get(position);
if(null != data)
{
TextView title = (TextView)view.findViewById(R.id.txtTitle);
TextView date = (TextView)view.findViewById(R.id.txtDate);
TextView description = (TextView)view.findViewById(R.id.txtDescription);
title.setText(data.title);
date.setText("on " + data.date);
description.setText(Html.fromHtml(data.description));
}
return view;
}
}
}
}
答案 0 :(得分:3)
像片段一样的static
内部类无法访问外部类实例OuterClass.this
。
对于片段中的Context
,请使用getActivity()
而不是OuterClass.this
。
答案 1 :(得分:0)
由于MyFragment
是静态类,因此您可以在News
中访问RetrieveRSSFeeds
活动上下文。
只需使用RetrieveRSSFeeds
类构造函数来获取doInBackground
中的Activity上下文:
public Context mContext;
public RetrieveRSSFeeds(Context mContext){
this.mContext=mContext;
}
来自活动:
fragment.new RetrieveRSSFeeds(News.this).execute();