0

i'm trying to run this function:

public static void main(String[] args) 
            throws ParserConfigurationException, SAXException, 
            IOException, XPathExpressionException {
                DocumentBuilderFactory domFactory = 
                DocumentBuilderFactory.newInstance();
                domFactory.setNamespaceAware(true); 
                DocumentBuilder builder = domFactory.newDocumentBuilder();
                Document doc = builder.parse("res/raw/test.xml");
                XPath xpath = XPathFactory.newInstance().newXPath();
                // XPath Query for showing all nodes value
                XPathExpression expr = xpath.compile("//station/*/text()");

                Object result = expr.evaluate(doc, XPathConstants.NODESET);
                NodeList nodes = (NodeList) result;
                for (int i = 0; i < nodes.getLength(); i++) {
                    System.out.println("zoekdit");
                    System.out.println(nodes.item(i).getNodeValue()); 
                }
             }

So I 've put it into the class: MainActivity. But when I'm searching in my logcat, I can't find "zoekdit" ...

1 Answers1

0

Android doesn't work that way, a method named main won't be called automatically.

For something like that you should run it in an AsyncTask or IntentService, so it doesn't hold up the UI thread. Google how to use AsyncTasks.

You would run the AsyncTask from inside of your activity. In the onResume method, or in a onClickListener of a button or something similar.

If you really want the code to run when the App starts, you can start a IntentService from the onCreate of the Application class. You need to create this application class. Extend from Application, and in the manifest, point the Application android:name parameter to the classpath of your created MyApplication class.

e.g

<!-- App manifest-->
<Application
   android:icon="@drawable/ic_launcher"
   android:name="com.myapp.myapplication"
>

<!-- declare activities-->
<Activity/>
</Application>
athor
  • 6,848
  • 2
  • 34
  • 37