2015-06-04 68 views
0

我正在Android應用程序與RSS工作室在Android工作室,所以我試圖把標題和描述:http://feeds.bbci.co.uk/news/rss.xml,以兩個文字使用自定義適配器。 rss供稿在一個標籤中。每次我嘗試運行我的程序時,我得到一個例外,在這個類中出現問題:Rss閱讀器與自定義陣列適配器

public class LAdapter extends ArrayAdapter<Info> { 
    public LAdapter(Context context, ArrayList<Info> nfos) {super(context, 0,nfos);} 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // Get the data item for this position 
     Info info = getItem(position); 
     // Check if an existing view is being reused, otherwise inflate the view 
     if (convertView == null) { 
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.basic_list_item, parent, false); 
     } 
     // Lookup view for data population 
     TextView title = (TextView) convertView.findViewById(R.id.Tit); 
     TextView description = (TextView) convertView.findViewById(R.id.Desc); 

     title.setText(info.Title); 
     description.setText(info.Description); 

     // Return the completed view to render on screen 
     return convertView; 
    } 
} 

部分Tab_1.class:

public class Tab_1 extends Fragment { 
ListView mList; 
ArrayList<Info> arrayOfInfo; 
LAdapter adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.tab_1, container, false); 
    LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo); 
#Line 44-> mList = (ListView) v.findViewById(R.id.list); 
    new GetRssFeed().execute("http://feeds.bbci.co.uk/news/rss.xml"); 
    mList.setAdapter(adapter); 
    return v; 
} 

例外:

Process: com.example.pauly.myapplication, PID: 3896 
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference 
     at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330) 
     at android.widget.ListView.setAdapter(ListView.java:487) 
     at com.example.pauly.myapplication.Tabs.Tab_1.onCreateView(Tab_1.java:43) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) 
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) 
     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490) 
     at 

android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:1072) 
      at android.support.v4.view.ViewPager.populate(ViewPager.java:918) 
      at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440) 
... 

回答

0
LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo); 

arrayOfInfo爲null(它甚至沒有初始化),因爲你在arrayOfInfo之後調用你的asynctask。隨着你從doInBackground方法得到的數據,你可以的AsyncTask與方法OnPostExecute數據集arrayOfInfo這個陣列設置的數據後,您可以在OnPostExecute打電話也

LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo); 
mList.setAdapter(adapter); 

OnPostExecute基本上是處理你的UI 。

+0

現在我得到這個:'java.lang.NullPointerException:嘗試在android.widget.ArrayAdapter.getCount(ArrayAdapter.java)空對象引用 上調用接口方法'int java.util.List.size()' :330) 在android.widget.ListView.setAdapter(ListView.java:487) 在com.example.pauly.myapplication.Tabs.Tab_1 $ GetRssFeed.onPostExecute(Tab_1.java:64) 在com.example.pauly .myapplication.Tabs.Tab_1 $ GetRssFeed.onPostExecute(Tab_1.java:44)'Java 64 is mList.setAdapter(adapter);在OnPostExecute –

+0

如何填充arrayList與此?:'RssReader rssReader = new RssReader(params [0]); (RssItem item:rssReader.getItems()){ new Info(item.getTitle(),item.getDescription());' –