在获取两个点之间的方向时获取android映射中的空指针异常

时间:2013-01-03 06:19:14

标签: android google-maps

我一直在关注this教程。

我在doc = (Document) urlConnection.getInputStream();得到一个空指针。

这是我的代码:

public class DirectionActivity extends MapActivity {
    MapView myMapView = null;
    MapController myMC = null;
    GeoPoint geoPoint = null;
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;
    HttpURLConnection urlConnection ;
    URL url ;
    String pathConent ;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myMapView = (MapView) findViewById(R.id.mapview1);
    geoPoint = null;
    myMapView.setSatellite(false);

    String pairs[] = getDirectionData("Ahmedabad", "Goa");
    String[] lngLat = pairs[0].split(",");

    // STARTING POINT
    GeoPoint startGP = new GeoPoint(
            (int) (Double.parseDouble(lngLat[1]) * 1E6), (int) (Double
                    .parseDouble(lngLat[0]) * 1E6));

    myMC = myMapView.getController();
    geoPoint = startGP;
    myMC.setCenter(geoPoint);
    myMC.setZoom(15);
    myMapView.getOverlays().add(new DirectionPathOverlay(startGP, startGP));

    // NAVIGATE THE PATH
    GeoPoint gp1;
    GeoPoint gp2 = startGP;

    for (int i = 1; i < pairs.length; i++) {
        lngLat = pairs[i].split(",");
        gp1 = gp2;
        // watch out! For GeoPoint, first:latitude, second:longitude
        gp2 = new GeoPoint((int) (Double.parseDouble(lngLat[1]) * 1E6),
                (int) (Double.parseDouble(lngLat[0]) * 1E6));
        myMapView.getOverlays().add(new DirectionPathOverlay(gp1, gp2));
        Log.d("xxx", "pair:" + pairs[i]);
    }

    // END POINT
    myMapView.getOverlays().add(new DirectionPathOverlay(gp2, gp2));

    myMapView.getController().animateTo(startGP);
    myMapView.setBuiltInZoomControls(true);
    myMapView.displayZoomControls(true);

}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

private String[] getDirectionData(String srcPlace, String destPlace) {

    String urlString = "http://maps.google.com/maps?f=d&hl=en&saddr="
            + srcPlace + "&daddr=" + destPlace
            + "&ie=UTF8&0&om=0&output=kml";
    Log.d("URL", urlString);

    try {

        url = new URL(urlString.toString());
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();

        dbf = DocumentBuilderFactory.newInstance();
        try {
            db = dbf.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ***doc = (Document) urlConnection.getInputStream();***(getting doc=null)


    } catch (Exception e) {
    }

    NodeList nl = doc.getElementsByTagName("LineString");
    for (int s = 0; s < nl.getLength(); s++) {
        Node rootNode = nl.item(s);
        NodeList configItems = rootNode.getChildNodes();
        for (int x = 0; x < configItems.getLength(); x++) {
            Node lineStringNode = configItems.item(x);
            NodeList path = lineStringNode.getChildNodes();
            pathConent = path.item(0).getNodeValue();
        }
    }
    String[] tempContent = pathConent.split(" ");
    return tempContent;
}

public class DirectionPathOverlay extends Overlay {
    private GeoPoint gp1;
    private GeoPoint gp2;
    public DirectionPathOverlay(GeoPoint gp1, GeoPoint gp2) {
        this.gp1 = gp1;
        this.gp2 = gp2;
    }

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {
        // TODO Auto-generated method stub
        Projection projection = mapView.getProjection();
        if (shadow == false) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            Point point = new Point();
            projection.toPixels(gp1, point);
            paint.setColor(Color.BLUE);
            Point point2 = new Point();
            projection.toPixels(gp2, point2);
            paint.setStrokeWidth(2);
            canvas.drawLine((float) point.x, (float) point.y, (float) point2.x,
                    (float) point2.y, paint);
        }
        return super.draw(canvas, mapView, shadow, when);
    }

    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {
        // TODO Auto-generated method stub

        super.draw(canvas, mapView, shadow);
    }

}
}

这是Logcat:

01-03 10:56:33.447: E/AndroidRuntime(1018): FATAL EXCEPTION: main
01-03 10:56:33.447: E/AndroidRuntime(1018): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.itsmyway/com.example.itsmyway.DirectionActivity}: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.os.Looper.loop(Looper.java:137)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at dalvik.system.NativeStart.main(Native Method)
01-03 10:56:33.447: E/AndroidRuntime(1018): Caused by: java.lang.NullPointerException
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.example.itsmyway.DirectionActivity.getDirectionData(DirectionActivity.java:109)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at com.example.itsmyway.DirectionActivity.onCreate(DirectionActivity.java:41)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.Activity.performCreate(Activity.java:5008)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-03 10:56:33.447: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-03 10:56:33.447: E/AndroidRuntime(1018):     ... 11 more

1 个答案:

答案 0 :(得分:1)

我已经实现了相同的示例并遇到了同样的问题。

您的应用程序的作用是从Google服务器检索文档,然后在该文档中查找说明。虽然kml文档(你的代码想要检索的文档)曾经工作了几个月(就像编写该教程时那样),但是自从Google于2012年12月更新了他们的API以来它现在无效。

解决方案:您的网址似乎已被弃用。谷歌地图使用JSON和XML而不是kml。 请查看此page以了解如何使用网络服务。

现在,

1)根据这些网络服务的指南创建一个URL。

2)现在向Google服务器发送HTTP连接请求,您必须获得输入流。


您的问题已经结束,但如果您在解决此问题后使用相同的代码,则会遇到另一个解析收到的文档的问题。

解决第二个问题:

1)将文件保存在手机的SD卡中并手动查看。存储文档的指南是here

2)根据this页面上的XML输出部分找到文档中的路线。

3)使用DocumentBuilderFactory或其他方式检索这些方向。

您的应用程序会查找标记&#34; LineString&#34;在kml文件中。但是,由于您现在使用的是XML或JSON,请查找指南中给出的相关标记。