I have an error when I run my AndroidXMLParsingActivity ( java.lang.RuntimeException: Unable to start activity)? .................................................. .....................................

01-22 17:51:12.516: E/AndroidRuntime(1610): FATAL EXCEPTION: main 01-22 17:51:12.516: E/AndroidRuntime(1610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dailytask/com.example.dailytask.AndroidXMLParsingActivity}: android.os.NetworkOnMainThreadException 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2211) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2261) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread.access$600(ActivityThre ad.java:141) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1256) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.os.Handler.dispatchMessage(Handler.java:99 ) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.os.Looper.loop(Looper.java:137) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread.main(ActivityThread.jav a:5103) 01-22 17:51:12.516: E/AndroidRuntime(1610): at java.lang.reflect.Method.invokeNative(Native Method) 01-22 17:51:12.516: E/AndroidRuntime(1610): at java.lang.reflect.Method.invoke(Method.java:525) 01-22 17:51:12.516: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:737) 01-22 17:51:12.516: E/AndroidRuntime(1610): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553) 01-22 17:51:12.516: E/AndroidRuntime(1610): at dalvik.system.NativeStart.main(Native Method) 01-22 17:51:12.516: E/AndroidRuntime(1610): Caused by: android.os.NetworkOnMainThreadException 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.os.StrictMode$AndroidBlockGuardPolicy.onNe twork(StrictMode.java:1133) 01-22 17:51:12.516: E/AndroidRuntime(1610): at java.net.InetAddress.lookupHostByName(InetAddress. java:385) 01-22 17:51:12.516: E/AndroidRuntime(1610): at java.net.InetAddress.getAllByNameImpl(InetAddress. java:236) 01-22 17:51:12.516: E/AndroidRuntime(1610): at java.net.InetAddress.getAllByName(InetAddress.java :214) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.conn.DefaultClientConnectionO perator.openConnection(DefaultClientConnectionOper ator.java:137) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.conn.AbstractPoolEntry.open(A bstractPoolEntry.java:164) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.conn.AbstractPooledConnAdapte r.open(AbstractPooledConnAdapter.java:119) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.client.DefaultRequestDirector .execute(DefaultRequestDirector.java:360) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:555) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:487) 01-22 17:51:12.516: E/AndroidRuntime(1610): at org.apache.http.impl.client.AbstractHttpClient.exe cute(AbstractHttpClient.java:465) 01-22 17:51:12.516: E/AndroidRuntime(1610): at com.example.dailytask.XMLParser.getXmlFromUrl(XMLP arser.java:40) 01-22 17:51:12.516: E/AndroidRuntime(1610): at com.example.dailytask.AndroidXMLParsingActivity.on Create(AndroidXMLParsingActivity.java:41) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.Activity.performCreate(Activity.java:5 133) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1087) 01-22 17:51:12.516: E/AndroidRuntime(1610): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2175) 01-22 17:51:12.516: E/AndroidRuntime(1610): ... 11 more XMLParser code

public class XMLParser { public String getXmlFromUrl(String url) { String xml = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); xml = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return xml; } public Document getDomElement(String xml) { Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(xml)); doc = db.parse(is); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage()); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage()); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage()); return null; } return doc; } public final String getElementValue( Node elem ) { Node child; if( elem != null){ if (elem.hasChildNodes()){ for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){ if( child.getNodeType() == Node.TEXT_NODE ){ return child.getNodeValue(); } } } } return ""; } public String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); } } AndroidXMLParsingActivity code

public class AndroidXMLParsingActivity extends ListActivity { // All static variables static final String URL = "http://www.androidituts.com/source/tutorials.xml"; // XML node keys static final String KEY_ITEM = "item"; // parent node static final String KEY_ID = "id"; static final String KEY_NAME = "name"; static final String KEY_CATEGORY = "category"; static final String KEY_PUB = "published"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.androidxmlparsing); ArrayList menuItems = new ArrayList(); XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Document doc = parser.getDomElement(xml); // getting DOM element NodeList nl = doc.getElementsByTagName(KEY_ITEM); // looping through all item nodes for (int i = 0; i < nl.getLength(); i++) { // creating new HashMap HashMap map = new HashMap(); Element e = (Element) nl.item(i); // adding each child node to HashMap key => value map.put(KEY_ID, parser.getValue(e, KEY_ID)); map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); map.put(KEY_CATEGORY, "Category:-" + parser.getValue(e, KEY_CATEGORY)); map.put(KEY_PUB, parser.getValue(e, KEY_PUB)); // adding HashList to ArrayList menuItems.add(map); } // Adding menuItems to ListView ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item, new String[] { KEY_NAME, KEY_CATEGORY, KEY_PUB }, new int[] { R.id.name, R.id.category, R.id.published }); setListAdapter(adapter); // selecting single ListView item ListView lv = getListView(); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { // getting values from selected ListItem String name = ((TextView) view.findViewById(R.id.name)).getText().toString() ; String cost = ((TextView) view.findViewById(R.id.category)).getText().toStri ng(); String description = ((TextView) view.findViewById(R.id.published)).getText().toStr ing(); // Starting new intent Intent in = new Intent(getApplicationContext(), SingleItemActivity.class); in.putExtra(KEY_NAME, name); in.putExtra(KEY_CATEGORY, cost); in.putExtra(KEY_PUB, description); startActivity(in); } }); } }

Check Solution