title: Parsing xml data to display in Fragment Android
body: <p>I am trying to parse xml from a Url and display in a fragment but it doesn't work. The application crashes each time I launch it. Any help please? Thanks</p>
  
  <p>Here is the code for the Adapter:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  import android.app.Activity;
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.ImageView;
  import android.widget.TextView;
  
  public class LazyAdapter extends BaseAdapter {
      private Activity activity;
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; data;
      private static LayoutInflater inflater=null;
      public ImageLoader imageLoader; 
  
      public LazyAdapter(Activity a, ArrayList&lt;HashMap&lt;String, String&gt;&gt; d) {
          activity = a;
          data=d;
          inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          imageLoader=new ImageLoader(activity.getApplicationContext());
      }
  
      public int getCount() {
          return data.size();
      }
  
      public Object getItem(int position) {
          return position;
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          View vi=convertView;
          if(convertView==null)
              vi = inflater.inflate(R.layout.list_row, null);
  
          TextView title = (TextView)vi.findViewById(R.id.title); // title
          TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
          TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
          ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
  
          HashMap&lt;String, String&gt; song = new HashMap&lt;String, String&gt;();
          song = data.get(position);
  
          // Setting all values in listview
          title.setText(song.get(HomeFragment.KEY_TITLE));
          artist.setText(song.get(HomeFragment.KEY_ARTIST));
          duration.setText(song.get(HomeFragment.KEY_DURATION));
          imageLoader.DisplayImage(song.get(HomeFragment.KEY_THUMB_URL), thumb_image);
          return vi;
      }
  }
  </code></pre>
  
  <p>Here is the code for the Fragment:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.HashMap;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.NodeList;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import android.widget.AdapterView.OnItemClickListener;
  
  public class HomeFragment extends Fragment {
      static final String URL = "http://api.androidhive.info/music/music.xml";
      // XML node keys
      static final String KEY_SONG = "song"; // parent node
      static final String KEY_ID = "id";
      static final String KEY_TITLE = "title";
      static final String KEY_ARTIST = "artist";
      static final String KEY_DURATION = "duration";
      static final String KEY_THUMB_URL = "thumb_url";
  
      ListView list;
      LazyAdapter adapter;
  
      public HomeFragment(){}
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.fragment_home, container, false);
  
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          XMLParser parser = new XMLParser();
          String xml = parser.getXmlFromUrl(URL); // getting XML from URL
          Document doc = parser.getDomElement(xml); // getting DOM element
  
          NodeList nl = doc.getElementsByTagName(KEY_SONG);
          // looping through all song nodes &lt;song&gt;
          for (int i = 0; i &lt; nl.getLength(); i++) {
              // creating new HashMap
              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
              Element e = (Element) nl.item(i);
              // adding each child node to HashMap key =&gt; value
              map.put(KEY_ID, parser.getValue(e, KEY_ID));
              map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
              map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
              map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
              map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
  
              // adding HashList to ArrayList
              songsList.add(map);
          }
  
          list=(ListView)getView().findViewById(R.id.list);
  
          // Getting adapter by passing xml data ArrayList
          adapter=new LazyAdapter(getActivity(), songsList);        
          list.setAdapter(adapter);
  
  
          // Click event for single list row
          list.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) {
  
  
              }
          }); 
  
          return rootView;
      }
  }
  </code></pre>