解析xml文件时出错

时间:2015-08-24 16:30:17

标签: android xml-parsing

我的应用需要在xml中下载一个rss文件。我解析数据,但我无法将它们插入列表视图。

我的代码是。

 public class MainActivity extends Activity {


    ProgressDialog progress =null;
    private final static String ADDRESS ="http://www.repubblica.it/rss/sport/rss2.0.xml";
    private ListView lista =null ;
    private List<ArticleInfo> list = null;
   private  ArrayAdapter<ArticleInfo> adapter = null ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        lista= (ListView)findViewById(R.id.listView);
        //adapter =new ArrayAdapter<ArticleInfo>(this,android.R.layout.simple_list_item_1,list);
       //lista.setAdapter(adapter);





        progress = new ProgressDialog(this);
        progress.setMax(100);
        progress.setMessage("Attendi qualche istante");



}

        public void start(View v){

            new BackgroundTask().execute();
        }



       private class BackgroundTask extends AsyncTask<String,Integer,String> {


           @Override
           protected String doInBackground(String... params) {

               URL url = null;
               try {
                   url = new URL(ADDRESS);
               } catch (MalformedURLException e) {
                   return null;
               }
               StringBuffer buffer=null;
               try {
                   BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
                   String tmp = null;
                   buffer = new StringBuffer();
                   while((tmp=reader.readLine())!= null){
                       buffer.append(tmp);

                   }
               } catch (IOException e) {
                   return null;
               }
              list = RssParser.parseXML(buffer.toString());

               return buffer.toString();

           }
           @Override
           protected  void onPreExecute(){

               progress.setProgress(0);
               progress.show();

           }
           @Override
           protected void onProgressUpdate(Integer ... values){

               super.onProgressUpdate(values);
               progress.setProgress(values[0]);
           }

           @Override
           protected void onPostExecute(String res){
            super.onPostExecute(res);
               progress.dismiss();
               if (res!=null){
                      // TextView text = (TextView)findViewById(R.id.text);
                        //text.setText(res);
                  adapter =new ArrayAdapter<ArticleInfo>(this,android.R.layout.simple_list_item_1,res);
                   lista.setAdapter(adapter);  **THIS IS MY ERROR** 

               }

           }



       }


   }

这是我的解析器类

public class RssParser {

public static List<ArticleInfo> parseXML(String rss){

        List<ArticleInfo> res =new ArrayList<>();
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder =null;

try {
    builder=factory.newDocumentBuilder();
} catch (ParserConfigurationException e) { }

try {
    Document doc = builder.parse(new InputSource(new StringReader(rss)));
    doc.getDocumentElement().normalize();// aggiungo getDocumentElement()
    NodeList list = doc.getElementsByTagName("item");
    for(int i=0;i<list.getLength();i++){

        Node n=list.item(i);
        if(n.getNodeType()==Node.ELEMENT_NODE){
            Element e= (Element) n;
            String title = e.getElementsByTagName("title").item(0).getTextContent();
            String url = e.getElementsByTagName("link").item(0).getTextContent();
            res.add(new ArticleInfo(title,url));
        }
    }
} catch (SAXException e) {

} catch (IOException e) {

}

return res;

}

框架在主要活动的最后一行给出了一个错误,我尝试创建适配器

你可以帮我一把吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

好吧我使用了上下文并且错误消失了。当我启动模拟器时,当asynctask工作时,应用程序崩溃。 logcat是j

ava.lang.NullPointerException
            at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
            at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
            at artetecapp.ilfattounofficial.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:123)
            at artetecapp.ilfattounofficial.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:73)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)