我创建了一个带抽屉菜单的应用程序,为每个菜单选项打开一个新片段。在这些片段中,我正在尝试运行几个函数。
我已经解析了一个RSS提要,并希望显示我在特定片段内的ListView上解析的信息。
我的主要活动导航到片段没问题,但卡住了空指针异常。
这是我缺乏Java知识的地方,因为我不确定如何纠正这些问题。
public class IncidentsFragment extends Fragment {
ListView lvData;
ArrayList<String> titles;
ArrayList<String> description;
ArrayList<String> pubdate;
public IncidentsFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_incidents, container, false);
titles = new ArrayList<String>();
description = new ArrayList<String>();
pubdate = new ArrayList<String>();
ListView lvData = (ListView) view.findViewById(R.id.lvData);
new ProcessInBackground().execute();
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, titles );
return view;
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (
IOException e)
{
return null;
}
}
public class ProcessInBackground extends AsyncTask<Integer, Void, Exception> {
ProgressDialog progressDialog = new ProgressDialog(getContext());
Exception exception = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading RSS Feed...");
progressDialog.show();
}
@Override
protected Exception doInBackground(Integer... integers) {
try {
URL url = new URL("https://trafficscotland.org/rss/feeds/currentincidents.aspx");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// creates new instance of pull parser factory that allows XML retrieval
factory.setNamespaceAware(false);
// parser produced does not support XML namespaces
XmlPullParser xpp = factory.newPullParser();
//new instance of parser, extracts xml document data
xpp.setInput(getInputStream(url), "UTF_8");
//encoding is in UTF8
boolean insideItem = false;
int eventType = xpp.getEventType();
//when we start reading, it returns the type of current event i.e. tag type
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
titles.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem) {
description.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("pubDate")) {
if (insideItem) {
pubdate.add(xpp.nextText());
}
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next();
}
} catch (MalformedURLException e) {
exception = e;
} catch (XmlPullParserException e) {
exception = e;
} catch (IOException e) {
exception = e;
}
return exception;
}
@Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, titles);
lvData.setAdapter(adapter);
//connects to data to the list view
progressDialog.dismiss();
}
}
}
我已经声明了listview和顶部的几个数组,它们将存储各个RSS标签,然后我使用Async任务来执行我的RSS解析。
这是logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: gcumwd.trafficscotlandapp, PID: 13433
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at gcumwd.trafficscotlandapp.IncidentsFragment$ProcessInBackground.onPostExecute(IncidentsFragment.java:153)
at gcumwd.trafficscotlandapp.IncidentsFragment$ProcessInBackground.onPostExecute(IncidentsFragment.java:83)
at android.os.AsyncTask.finish(AsyncTask.java:667)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:684)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
非常感谢任何帮助。我所要做的就是解析RSS提要并将数据显示在片段内的ListView上。
答案 0 :(得分:1)
问题是您已经在片段中声明了列表视图,并且在onCreateView中再次声明了它。您的onCreateView代码应如下所示:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_incidents, container, false);
titles = new ArrayList<String>();
description = new ArrayList<String>();
pubdate = new ArrayList<String>();
lvData = (ListView) view.findViewById(R.id.lvData);
new ProcessInBackground().execute();
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1, titles );
return view;
}