0

Hello I have been working on this project for a while and am very stuck at the moment, I am still new to Android and have been working on my first app which pulls information from an XML file that I create with MediaWiki API. Every time that I try to select the "Players" tab on my app, it force closes and I cannot figure out why. Here is the code that I have for my players Activity and Wikiparser activity:

package com.lvlup.kikurself.scotttest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import java.io.IOException;
import org.xml.sax.SAXException;

//import com.lvlup.kikurself.scotttest.WikiParser.Cm;

public class scottPlayers extends ListActivity {
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

WikiParser p = new WikiParser();
ArrayList<String> titles = new ArrayList<String>();

try {
    p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);

} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

    //String[] values = new String[50]; 
    //values = res;




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, titles);

    setListAdapter(adapter);

    final ListView playersList = getListView();


    playersList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long thisID)
    {
         Object o = (playersList.getItemAtPosition(position));
         String playerName_temp = (o.toString());

         Intent newIntent = new Intent(v.getContext(), playerDisp.class);
         newIntent.putExtra("tempN", playerName_temp);
         startActivity(newIntent);

    }
    });



    //get your data back again with: String fName = getIntent().getExtras().getInt("fname");




}}

And next the code for the WikiParser function:

package com.lvlup.kikurself.scotttest;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.sax.*;
import android.text.TextUtils;
import android.util.Xml;
import android.util.Xml.Encoding;

public class WikiParser {
private static class CmListener implements StartElementListener {
    final List<String> mTitles;
    CmListener(List<String> titles) {
        mTitles = titles;
    }
    @Override
    public void start(Attributes attributes) {
        String title = attributes.getValue("", "title");
        if (!TextUtils.isEmpty(title)) {
            mTitles.add(title);
        }
    }
}
public void parseInto(URL url, List<String> titles) throws IOException, SAXException {
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    try {
        parseInto(new BufferedInputStream(con.getInputStream()), titles);
    } finally {
        con.disconnect();
    }
}
public void parseInto(InputStream docStream, List<String> titles) throws IOException, SAXException {
    RootElement api = new RootElement("api");
    Element query = api.requireChild("query");
    Element categoryMembers = query.requireChild("categorymembers");
    Element cm = categoryMembers.requireChild("cm");
    cm.setStartElementListener(new CmListener(titles));
    Xml.parse(docStream, Encoding.UTF_8, api.getContentHandler());
}
}

There it is..I am here to learn my mistakes so anything that anyone can tell me about my code and what I can do to improve and if anyone has an idea whats going wrong, thank you in advance.

EDIT: Including LogCat also, thank you.

    08-06 04:33:58.837: D/dalvikvm(539): GC_FOR_ALLOC freed 62K, 3% free 9883K/10179K, paused 64ms
08-06 04:33:58.917: I/dalvikvm-heap(539): Grow heap (frag case) to 13.641MB for 4096016-byte allocation
08-06 04:33:59.037: D/dalvikvm(539): GC_CONCURRENT freed <1K, 3% free 13882K/14215K, paused 12ms+17ms
08-06 04:33:59.748: D/gralloc_goldfish(539): Emulator without GPU emulation detected.
08-06 04:34:05.977: D/AndroidRuntime(539): Shutting down VM
08-06 04:34:05.987: W/dalvikvm(539): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-06 04:34:06.067: E/AndroidRuntime(539): FATAL EXCEPTION: main
08-06 04:34:06.067: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lvlup.kikurself.scotttest/com.lvlup.kikurself.scotttest.scottPlayers}: android.os.NetworkOnMainThreadException
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.Looper.loop(Looper.java:137)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.main(ActivityThread.java:4340)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.lang.reflect.Method.invokeNative(Native Method)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.lang.reflect.Method.invoke(Method.java:511)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-06 04:34:06.067: E/AndroidRuntime(539):  at dalvik.system.NativeStart.main(Native Method)
08-06 04:34:06.067: E/AndroidRuntime(539): Caused by: android.os.NetworkOnMainThreadException
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-06 04:34:06.067: E/AndroidRuntime(539):  at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-06 04:34:06.067: E/AndroidRuntime(539):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.lvlup.kikurself.scotttest.WikiParser.parseInto(WikiParser.java:35)
08-06 04:34:06.067: E/AndroidRuntime(539):  at com.lvlup.kikurself.scotttest.scottPlayers.onCreate(scottPlayers.java:29)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.Activity.performCreate(Activity.java:4465)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-06 04:34:06.067: E/AndroidRuntime(539):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-06 04:34:06.067: E/AndroidRuntime(539):  ... 11 more
kikurself
  • 153
  • 1
  • 10

2 Answers2

2

NetworkOnMainThreadException

The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.

Also see StrictMode.

Have a look at existing answers on Stackoverflow -

  1. NetworkOnMainThreadException

  2. android.os.NetworkOnMainThreadException

and for more details you can get here

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Okay thank you very much for that! I was unaware. So basically I just need to separate the functions I have and keep UI functions in the Main thread and create a child thread to handle network comm. and things like that right? – kikurself Aug 06 '12 at 14:18
  • Yes, you've to use thread to handle this. Just refer the existing answers. – Praveenkumar Aug 07 '12 at 04:19
1
  1. Its always advisable to keep the UI work on UI thread, and Non-UI work on Non-UI thread, but that became a Law with the arrival of HoneyComb version of Android.

  2. Application in android starts with the Dedicated UI thread, so when you create another thread to handle long process work like network, fileIO etc, you get dropped of the Dedicated UI thread.

  3. To sync the UI thread and the Non-UI thread, use Thread with a Handler or AsyncTask which is specially introduced into android to give easy sync between the UI and Non-UI thread, also called as Painless Threading....

svick
  • 236,525
  • 50
  • 385
  • 514
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75