我正在创建一个Android工作室应用程序,其中包含选项卡,其中一个选项卡有一个RSS提要,我正试图将帖子的标题和说明从http://feeds.bbci.co.uk/news/rss.xml添加到列表中查看,但我得到一个例外:
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$GetRssFeed.onPostExecute(Tab_1.java:65)
at com.example.pauly.myapplication.Tabs.Tab_1$GetRssFeed.onPostExecute(Tab_1.java:45)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Tab_1.java:
public class Tab_1 extends Fragment {
ListView mList;
ArrayList<Info> arrayOfInfo;
int i=0;
@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);
mList = (ListView) v.findViewById(R.id.list);
new GetRssFeed().execute("http://feeds.bbci.co.uk/news/rss.xml");
return v;
}
l.45- private class GetRssFeed extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
try {
RssReader rssReader = new RssReader(params[0]);
for (RssItem item : rssReader.getItems()) {
arrayOfInfo.add(i++, new Info(item.getTitle(), item.getDescription()));
}
} catch (Exception e) {
Log.v("Error Parsing Data", e + "");
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo);
adapter.notifyDataSetChanged();
l.65 mList.setAdapter(adapter);
}
}
}
答案 0 :(得分:0)
您需要指定ArrayList对象;
ArrayList<Info> arrayOfInfo;
replaceWith
ArrayList<Info> arrayOfInfo=new ArrayList<>();