Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:03:22+00:00 2026-05-30T20:03:22+00:00

Okay. I have a main UI with buttons on it. When a user clicks

  • 0

Okay. I have a main UI with buttons on it. When a user clicks one of the buttons it should bring up a listview created by a separate activity. When I run this activity by itself, as it’s own app, it’s works just fine, but when I try to use it as component of a larger app, it shuts the app down. Here’s the code for the activity I’m trying to call:

table.java

public class Table extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_table);
    ListView lv= (ListView)findViewById(R.id.listview);

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


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

    int numResults = XMLfunctions.numResults(doc);

    if((numResults <= 0)){
        Toast.makeText(Table.this, "Nothing to show.", Toast.LENGTH_LONG).show();  
        finish();
    }

    NodeList nodes = doc.getElementsByTagName("team");

    for (int i = 0; i < nodes.getLength(); i++) {                           
        HashMap<String, String> map = new HashMap<String, String>();    

        Element e = (Element)nodes.item(i);
        map.put("rowid", XMLfunctions.getValue(e, "id"));           
        map.put("name", XMLfunctions.getValue(e, "name"));
        map.put("w",  XMLfunctions.getValue(e, "w"));
        map.put("d",  XMLfunctions.getValue(e, "d"));
        map.put("l",  XMLfunctions.getValue(e, "l"));
        map.put("gd",  XMLfunctions.getValue(e, "gd"));
        map.put("pts", XMLfunctions.getValue(e, "pts"));
        mylist.add(map);            
    }       


    //Make a new listadapter
    ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.grid_item,
                    new String[] { "rowid", "name", "w" , "d", "l", "gd", "pts"},
                    new int[] {R.id.item1, R.id.item2, R.id.item3, R.id.item4, R.id.item5, R.id.item6, R.id.item7 });

       lv.setAdapter(adapter);


}

The table.java activity parses the xml data that this class pulls:

XMLfunctions.java

public class XMLfunctions {

public final static Document XMLfromString(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) {
        System.out.println("XML parse error: " + e.getMessage());
        return null;
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("I/O exeption: " + e.getMessage());
        return null;
    }

    return doc;

}

/** Returns element value
  * @param elem element (it is XML tag)
  * @return Element value otherwise empty String
  */
 public final static String getElementValue( Node elem ) {
     Node kid;
     if( elem != null){
         if (elem.hasChildNodes()){
             for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
                 if( kid.getNodeType() == Node.TEXT_NODE  ){
                     return kid.getNodeValue();
                 }
             }
         }
     }
     return "";
 }

 public static String getXML(){  
        String line = null;

        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://adasoccerclub.org/get_json.php");

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (MalformedURLException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        } catch (IOException e) {
            line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
        }

        return line;

}

public static int numResults(Document doc){     
    Node results = doc.getDocumentElement();
    int res = -1;

    try{
        res = Integer.valueOf(results.getAttributes().getNamedItem("count").getNodeValue());
    }catch(Exception e ){
        res = -1;
    }

    return res;
}

public static String getValue(Element item, String str) {       
    NodeList n = item.getElementsByTagName(str);        
    return XMLfunctions.getElementValue(n.item(0));
}

Finally, here’s the LogCat from the debug:

03-05 23:43:54.858: E/AndroidRuntime(9190): FATAL EXCEPTION: main

03-05 23:43:54.858: E/AndroidRuntime(9190): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.club/com.example.club.Table}: android.os.NetworkOnMainThreadException

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread.access$600(ActivityThread.java:122)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.os.Handler.dispatchMessage(Handler.java:99)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.os.Looper.loop(Looper.java:137)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread.main(ActivityThread.java:4340)

03-05 23:43:54.858: E/AndroidRuntime(9190): at java.lang.reflect.Method.invokeNative(Native Method)

03-05 23:43:54.858: E/AndroidRuntime(9190): at java.lang.reflect.Method.invoke(Method.java:511)

03-05 23:43:54.858: E/AndroidRuntime(9190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

03-05 23:43:54.858: E/AndroidRuntime(9190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

03-05 23:43:54.858: E/AndroidRuntime(9190): at dalvik.system.NativeStart.main(Native Method)

03-05 23:43:54.858: E/AndroidRuntime(9190): Caused by: android.os.NetworkOnMainThreadException

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)

03-05 23:43:54.858: E/AndroidRuntime(9190): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)

03-05 23:43:54.858: E/AndroidRuntime(9190): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)

03-05 23:43:54.858: E/AndroidRuntime(9190): at java.net.InetAddress.getAllByName(InetAddress.java:220)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

03-05 23:43:54.858: E/AndroidRuntime(9190): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

03-05 23:43:54.858: E/AndroidRuntime(9190): at com.example.club.ClubActivity$XMLfunctions.getXML(ClubActivity.java:205)

03-05 23:43:54.858: E/AndroidRuntime(9190): at com.example.club.MainTable.onCreate(Table.java:30)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.Activity.performCreate(Activity.java:4465)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

03-05 23:43:54.858: E/AndroidRuntime(9190): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

Thanks for any and all help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:03:22+00:00Added an answer on May 30, 2026 at 8:03 pm

    If you see your logcat, that itself says NetworkOnMainThreadException at com.example.club.ClubActivity$XMLfunctions.getXML(ClubActivity.java:205). In getXML, you are doing network operation which is heavy operation in nature. You only can think of it like if you getXML which you are calling in onCreate of Activity is taking so much time to load/fetch data from server, it will halt the Activity to come to full visible state and you may definitely get famous ANR (Application Not Responding) dialog. So to avoid this, android doc suggests to do heavy operations (like network operations) in background thread so that UI renders within specified time.

    So its better you change your logic and fetch network data using AsyncTask or a new thread with the help of Handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is a bit messy: I'm using Netbeans, and I have a main
Okay so basically : i have this simple example: main.cpp using namespace VHGO::Resource; std::list<BaseTable*>
Okay, I'm losing my mind over this one. I have a method in my
How can I re-use a function? Okay lets say I have this main function
Okay here's the program I have typed up(stdio.h is included also): /* function main
Okay I have this RewriteRule which is supposed to redirect any request for the
okay i have found the way to run a video in a image.... the
I used to think that an assembly could have only one main() method until
Okay, so you have a load of methods sprinkled around your system's main class.
Okay I am newer to python and have been researching this problem but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.