-1

Dear Stackoverflow programmers,

I've made an app a few months ago and it is working flawelessly. However, on the new Android ICS it started crashing. I looked it up and i get a Network On Main Thread Exception. I tried to rewrite my code but i can't get it to work the way it did.. I tried AsyncTasking but that also didn't work.. Please can someone help me out??

my code: (if you also need XMLFunctions.class , please let me know)

package test.lmc;

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.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class nieuwsflits extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.nieuwsflits);

    String xml = XMLfunctions.getXML();
    Document doc = XMLfunctions.XMLfromString(xml);

    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(nieuwsflits.this, "Nog geen resultaten gevonden...", Toast.LENGTH_LONG).show();  
        finish(); 
    }

    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    NodeList nodes = doc.getElementsByTagName("message");
    int max = 10;
    //fill in the list items from the XML document
    for (int i = 0; i < nodes.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("id", XMLfunctions.getValue(e, "id"));
        map.put("datum", XMLfunctions.getValue(e, "datum"));
        map.put("message-text", XMLfunctions.getValue(e, "message-text"));
        map.put("url", XMLfunctions.getValue(e, "url"));

        mylist.add(map);
    }       


    //Make a new listadapter
    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.listadapter,
                    new String[] { "message-text", "datum" },
                    new int[] { R.id.item_title, R.id.item_subtitle });

    setListAdapter(adapter);

    final ListView lv = getListView();
    lv.setTextFilterEnabled(true);  
    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
            String url = o.get("url");
            String check ="";
            if ((url == check)) {
                Toast.makeText(nieuwsflits.this, "Geen website gevonden...", Toast.LENGTH_LONG).show();
            }
            else {
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
            }
            /** Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(o.get("url"))); 
            startActivity(browserIntent); 
            Toast.makeText(nieuwsflits.this, "ID '" + o.get("url") + "' was clicked.", Toast.LENGTH_LONG).show(); */

            }
        });


    }

}

AakashM
  • 62,551
  • 17
  • 151
  • 186
Joris Lamers
  • 21
  • 1
  • 2

2 Answers2

0

My guess would be that in your XMLfunctions.getXML(); method you are performing a network call to retrieve your XML. This is being called from onCreate() which is executed on the UI thread. ICS is very strict about not letting you perform network requests on the UI thread, so you will need to move this to an AsyncTask or Loader.

Mark Allison
  • 21,839
  • 8
  • 47
  • 46
  • True, my `XMLfunctions.getXML();` is performing a network call. Point is, that I can't get it to work with an AsyncTask because my UI crashes and gets a lot of NullPointerExceptions, i thinks because my xml gets parsed afther my UI is called. I know it must be possible but i'm doing something wrong i guess.. any suggestions? – Joris Lamers Jan 31 '12 at 17:04
0

You will need to code your UI to cope with not having any valid data to begin with. Then after your AsyncTask has completed retrieving the data, it can request a refresh of the UI to display the valid data.

I’d suggest your UI initially show an indefinite progress bar (with the rotating dashes), perhaps along with the text “Loading...” to let the user know they have to wait; these can be hidden once the UI has valid data to show.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13