Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在片段上显示RSS提要_Android_Android Fragments_Android Asynctask_Nullpointerexception_Rss Reader - Fatal编程技术网

Android 在片段上显示RSS提要

Android 在片段上显示RSS提要,android,android-fragments,android-asynctask,nullpointerexception,rss-reader,Android,Android Fragments,Android Asynctask,Nullpointerexception,Rss Reader,我创建了一个带有抽屉菜单的应用程序,它为每个菜单选项打开一个新片段。在这些片段中,我尝试运行几个函数 我已经解析了一个RSS提要,希望在一个特定片段中的ListView上显示我解析的信息 我的主要活动导航到片段没有问题,但是遇到了空指针异常 这就是我缺乏Java知识的原因,因为我不确定如何纠正这些问题 public class IncidentsFragment extends Fragment { ListView lvData; ArrayList<String>

我创建了一个带有抽屉菜单的应用程序,它为每个菜单选项打开一个新片段。在这些片段中,我尝试运行几个函数

我已经解析了一个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();
        }
    }


}

非常感谢您的帮助。我所要做的就是解析RSS提要,并将数据显示在片段中的ListView上。

问题是您已经在片段中声明了列表视图,而在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;

}
@Nullable
@凌驾
创建视图时的公共视图(LayoutFlater充气机、@Nullable ViewGroup容器、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u事故,容器,错误);
titles=新的ArrayList();
description=新建ArrayList();
pubdate=newarraylist();
lvData=(ListView)view.findViewById(R.id.lvData);
新建ProcessInBackground().execute();
//ArrayAdapter=新的ArrayAdapter(getContext(),android.R.layout.simple\u list\u item\u 1,titles);
返回视图;
}

我猜
lvData.setAdapter(适配器)是第153行吗?您的例外情况是
lvData
为空。尝试不要在onCreateView中声明本地
lvData
,而是初始化类中的字段。非常感谢!我花了几个小时试图找出它的原因,但从来没有想过我已经声明了两次
@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;

}