Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Maps Api v2 XML解析问题_Java_Android_Xml_Google Maps_Xml Parsing - Fatal编程技术网

Java Android Maps Api v2 XML解析问题

Java Android Maps Api v2 XML解析问题,java,android,xml,google-maps,xml-parsing,Java,Android,Xml,Google Maps,Xml Parsing,我一直在尝试解析一个由google()自动生成的XML 这正是我作为程序示例使用的XML: 我的代码: public class MapDirection { public final static String MODE_DRIVING = "driving"; public final static String MODE_WALKING = "walking"; public MapDirection() { } public Document getDocument(LatLng

我一直在尝试解析一个由google()自动生成的XML

这正是我作为程序示例使用的XML:

我的代码:

public class MapDirection {
public final static String MODE_DRIVING = "driving";
public final static String MODE_WALKING = "walking";

public MapDirection() { }

public Document getDocument(LatLng start, LatLng end, String mode) {
    try {
        URL url = new URL("http://maps.googleapis.com/maps/api/directions/xml?" 
            + "origin=" + start.latitude + "," + start.longitude  
            + "&destination=" + end.latitude + "," + end.longitude 
            + "&sensor=false&units=metric&mode=driving");
        DocumentBuilderFactory mDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder mDocumentBuilder = mDocumentBuilderFactory.newDocumentBuilder();
        Document mDocument = mDocumentBuilder.parse(new InputSource(url.openStream()));
        mDocument.getDocumentElement().normalize();
        return mDocument;
    }
    catch (Exception e) {
        System.out.println("XML Parsing Exception = " + e);
    }

    return null;
}

public String getDurationText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDurationValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "duration"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getDistanceText (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "text"));
    return node4.getTextContent();
}

public int getDistanceValue (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "distance"));
    NodeList nodeList4 = node3.getChildNodes();
    Node node4 = nodeList4.item(getNodeIndex(nodeList4, "value"));
    return Integer.parseInt(node4.getTextContent());
}

public String getStartAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "start_address"));
    return node3.getTextContent();
}

public String getEndAddress (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "leg"));
    NodeList nodeList3 = node2.getChildNodes();
    Node node3 = nodeList3.item(getNodeIndex(nodeList3, "end_address"));
    return node3.getTextContent();
}

public String getCopyRights (Document mDocument) {
    NodeList nodeList1 = mDocument.getElementsByTagName("DirectResponse");
    Node node1 = nodeList1.item(getNodeIndex(nodeList1, "route"));
    NodeList nodeList2 = node1.getChildNodes();
    Node node2 = nodeList2.item(getNodeIndex(nodeList2, "copyrights"));
    return node2.getTextContent();
}
private int getNodeIndex(NodeList nodeList, String nodeName) {
    for(int i = 0 ; i < nodeList.getLength() ; i++) {
        if(nodeList.item(i).getNodeName().equals(nodeName))
            return i;
    }
    return -1;
}
我的最后一个问题是,我做错了什么?我得到的错误是:

09-13 10:52:57.791: E/AndroidRuntime(13720): FATAL EXCEPTION: main
09-13 10:52:57.791: E/AndroidRuntime(13720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raulbutuc.taxifinder/com.raulbutuc.menu.items.OpenMap}: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Looper.loop(Looper.java:137)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at dalvik.system.NativeStart.main(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720): Caused by: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.GPSMapping.MapDirection.getDurationText(MapDirection.java:63)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.getRoute(OpenMap.java:170)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.onCreate(OpenMap.java:163)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Activity.performCreate(Activity.java:5008)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-13 10:52:57.791: E/AndroidRuntime(13720):    ... 11 more

我也有同样的问题。解决方案是将getDocument方法(以及所有相应的方法)转换为静态。

我同时使用JSON解析方法解决了这个问题,但非常感谢,事实上,这就是问题所在:)
09-13 10:52:57.791: E/AndroidRuntime(13720): FATAL EXCEPTION: main
09-13 10:52:57.791: E/AndroidRuntime(13720): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.raulbutuc.taxifinder/com.raulbutuc.menu.items.OpenMap}: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.os.Looper.loop(Looper.java:137)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.main(ActivityThread.java:4931)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invokeNative(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at java.lang.reflect.Method.invoke(Method.java:511)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at dalvik.system.NativeStart.main(Native Method)
09-13 10:52:57.791: E/AndroidRuntime(13720): Caused by: java.lang.NullPointerException
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.GPSMapping.MapDirection.getDurationText(MapDirection.java:63)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.getRoute(OpenMap.java:170)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at com.raulbutuc.menu.items.OpenMap.onCreate(OpenMap.java:163)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Activity.performCreate(Activity.java:5008)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-13 10:52:57.791: E/AndroidRuntime(13720):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
09-13 10:52:57.791: E/AndroidRuntime(13720):    ... 11 more