0
votes

I'm making an android app with a rss feed in android studio and so I'm trying to put the title and description from: http://feeds.bbci.co.uk/news/rss.xml, to two textviews using a custom adapter. The rss feed is in a tab. And each time I try to run my program I get an exception and the problem occurs in this class:

    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;
    }
}

Partial 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;
}

Exception:

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)
...
1

1 Answers

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

arrayOfInfo is null (it's not even initialized), because you call your asynctask after arrayOfInfo. With the data you receive from the DoInBackground method, you can set arrayOfInfo with data in the method OnPostExecute in asynctask and after this array is set with data, you can call in OnPostExecute also

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

OnPostExecute is basically handling your UI.