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!

2
  • 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. Commented Apr 5, 2013 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. Commented Apr 5, 2013 at 18:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why downvote...Let me know If I was wrong..Don't downvote intentionally
i'm not really getting what you're saying to me. i'm pretty new to this kind of development would you be able to be more specific?
You're not justifying why he's getting an exception.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.