2014-09-22 81 views
0

我試圖添加到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; 
       } 
     } 


} 





} 
+1

,而不是傳遞到適配器類 – Raghunandan 2014-09-22 07:45:43

回答

3

A static像你的片段一樣的內部類不能訪問外部類實例OuterClass.this

對於片段中的Context,使用getActivity()而不是OuterClass.this

+0

的構造'News.this'使用上下文謝謝你的工作! – 2014-09-22 08:21:03

0

因爲MyFragment是靜態類,因此您可以訪問NewsRetrieveRSSFeeds中的活動上下文。

只需使用RetrieveRSSFeeds類的構造函數來獲得活動上下文doInBackground

public Context mContext; 
public RetrieveRSSFeeds(Context mContext){ 
    this.mContext=mContext; 
} 

和活動:

fragment.new RetrieveRSSFeeds(News.this).execute(); 
+0

謝謝,這個作品,但@Iaalto的方式更容易 – 2014-09-22 08:21:29

相關問題