2

在过去的几天里,我一直在尝试修复这个运行时错误,但无济于事,不知道为什么它在调用 getDirection 方法时创建 NodeList 时会抛出 NullPointerException。我对Android编程很陌生,所以如果有人能给我一些指导,将不胜感激,谢谢!

编辑:通过调试器判断 LatLng 坐标和方向类型正在通过 getDocument() 方法传递,但没有从 getDocument() 方法返回文档。

public Document getDocument(LatLng start, LatLng end, String mode) {
    String 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";

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse response = httpClient.execute(httpPost, localContext);
        InputStream in = response.getEntity().getContent();
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(in);
        return doc;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

.

public ArrayList<LatLng> getDirection (Document doc) {
    NodeList nl1, nl2, nl3, nl4;
    ArrayList<LatLng> listGeopoints = new ArrayList<LatLng>();
    double time=0;
    nl1 = doc.getElementsByTagName("step");
    if (nl1.getLength() > 0) {
        for (int i = 0; i < nl1.getLength(); i++) {
        double t=0;
            Node node1 = nl1.item(i);
            nl2 = node1.getChildNodes();

            Node locationNode = nl2.item(getNodeIndex(nl2, "start_location"));
            nl3 = locationNode.getChildNodes();
            Node latNode = nl3.item(getNodeIndex(nl3, "lat"));
            double lat = Double.parseDouble(latNode.getTextContent());
            Node lngNode = nl3.item(getNodeIndex(nl3, "lng"));
            double lng = Double.parseDouble(lngNode.getTextContent());
            listGeopoints.add(new LatLng(lat, lng));

            locationNode = nl2.item(getNodeIndex(nl2, "polyline"));
            nl3 = locationNode.getChildNodes();
            latNode = nl3.item(getNodeIndex(nl3, "points"));
            ArrayList<LatLng> arr = decodePoly(latNode.getTextContent());
            for(int j = 0 ; j < arr.size() ; j++) {
                listGeopoints.add(new LatLng(arr.get(j).latitude, arr.get(j).longitude));
            }

            locationNode = nl2.item(getNodeIndex(nl2, "end_location"));
            nl3 = locationNode.getChildNodes();
            latNode = nl3.item(getNodeIndex(nl3, "lat"));
            lat = Double.parseDouble(latNode.getTextContent());
            lngNode = nl3.item(getNodeIndex(nl3, "lng"));
            lng = Double.parseDouble(lngNode.getTextContent());
            listGeopoints.add(new LatLng(lat, lng));


            locationNode = nl2.item(getNodeIndex(nl2, "duration"));
            nl4 = locationNode.getChildNodes();
            Node node2 = nl4.item(getNodeIndex(nl4, "value"));
            t = Double.parseDouble(node2.getTextContent());
            time=time+t;
        }
        Log.i("duration", String.valueOf(time));
    }

    return listGeopoints;
}

这是我用来调用 getDirection 方法的函数:

public void onClick_GetDirections(View v){
MyGPSToolDirections md = new MyGPSToolDirections();

LatLng fromPosition = new LatLng(13.68714, 100.53525);
LatLng toPosition = new LatLng(13.68366, 100.53900);

Document doc1 = md.getDocument(fromPosition, toPosition,     MyGPSToolDirections.MODE_DRIVING);

ArrayList<LatLng> directionPoint = md.getDirection(doc1);
PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

for(int i = 0 ; i < directionPoint.size() ; i++) {          
rectLine.add(directionPoint.get(i));

GPSToolMap.addPolyline(rectLine);
} 

堆栈跟踪:

08-07 17:14:31.682: E/AndroidRuntime(2201): FATAL EXCEPTION: main
08-07 17:14:31.682: E/AndroidRuntime(2201): java.lang.IllegalStateException: Could not execute method of the activity
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View$1.onClick(View.java:3599)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View.performClick(View.java:4204)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.view.View$PerformClick.run(View.java:17355)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Handler.handleCallback(Handler.java:725)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.os.Looper.loop(Looper.java:137)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invoke(Method.java:511)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at dalvik.system.NativeStart.main(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.reflect.InvocationTargetException
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at java.lang.reflect.Method.invoke(Method.java:511)
08 -07 17:14:31.682: E/AndroidRuntime(2201):    at android.view.View$1.onClick(View.java:3594)
08-07 17:14:31.682: E/AndroidRuntime(2201):     ... 11 more
08-07 17:14:31.682: E/AndroidRuntime(2201): Caused by: java.lang.NullPointerException
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.L00081183.mygpstool.MyGPSToolDirections.getDirection(MyGPSToolDirections.java:54)
08-07 17:14:31.682: E/AndroidRuntime(2201):     at com.L00081183.mygpstool.MainActivity.onClick_GetDirections(MainActivity.java:145)
4

0 回答 0