1

At the moment I have 14 elements added to an array list but I want them to be displayed in a listview activity so far I have:

public class ListView extends ListActivity{

  static final String baseURL ="http://www.dublincity.ie/dublintraffic/cpdata.xml?1354720067473";
  TextView tvcp;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    StringBuilder URL = new StringBuilder(baseURL);
    String fullURL = URL.toString();
    try{
      URL website = new URL(fullURL);
      SAXParserFactory spf = SAXParserFactory.newInstance();
      SAXParser sp = spf.newSAXParser();
      XMLReader xr = sp.getXMLReader();
      HandelingXML gettingData = new HandelingXML();
      xr.setContentHandler(gettingData);
      xr.parse(new InputSource(website.openStream()));

      ArrayList<CarparkNode>carparks = gettingData.getCarparks();

      this.setListAdapter(
        new ArrayAdapter<CarparkNode>(ListView.this,R.layout.listview, android.R.id.list, carparks)
      );
    }catch(Exception e){
      tvcp.setText("error");
    }
  }
}

However, every time I run this it will crash and I have no idea why as i have done this before using a simple array. If someone could point out what i'm doing wrong and give a solution that would be great thanks!

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • The LogCat will tell you what went wrong, but such a lightweight catch block will make that difficult. Add `e.printStackTrace()` to you catch block and read through the new stack trace, you might see a NetworkOnMainThreadException. After you've done this, click [edit] to post the warnings and errors you see so we don't have to guess. – Sam Apr 05 '13 at 18:21
  • You cannot perform networking operations on `Main Thread`. You need to move your parsing into background Thread for example `AsyncTask` - that also allows `UI` updates after work is done. – Simon Dorociak Apr 05 '13 at 18:23

2 Answers2

4

First don't give class name ListView Which is predefined class in Android

second Don't perform Network Operation on UI Thread.

Edit:

Reason:

Android version 3.x or more devices won't allow network operation to perform in the UI Thread, when you try to do that it will throw an exception saying you are performing network operation on the UI Thread, i.e NetworkOnMainThreadException. So What you need to do is to Create a separate thread to execute network operation or AsyncTask.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
0

Your crash is most likely android.os.NetworkOnMainThreadException getting thrown. Apps run in strict mode now by default, it's so the OS can let you know that you are doing something very very wrong.

You need make any and all network requests on a background thread, and not on the UI thread which will cause the UI to not respond to the user while the network requests are going, and possibly cause ANR dialogs.

See the answers to this question about how to fix your code to comply with best practices.

Community
  • 1
  • 1
Steven Byle
  • 13,149
  • 4
  • 45
  • 57