-1

程序不是错误,而是关于索引数组列表的 logcat 错误,所以图片我无法解决。请帮我。

public class MainActivity extends MapActivity {

@Override

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    MapView mv = (MapView) findViewById(R.id.mapview);

    mv.setBuiltInZoomControls(true);

    MapController mc = mv.getController();

    ArrayList all_geo_points = getDirections(16.821219, 100.260457, 13.913698, 100.510954);

    GeoPoint moveTo = (GeoPoint) all_geo_points.get(0);

    mc.animateTo(moveTo);

    mc.setZoom(12);

    mv.getOverlays().add(new MyOverlay(all_geo_points));

}


@Override

protected boolean isRouteDisplayed() {


    return false;

}



public static ArrayList getDirections(double lat1, double lon1, double lat2, double lon2) {

    String url = "http://maps.googleapis.com/maps/api/directions/xml?origin=" +lat1 + "," + lon1  + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";

    String tag[] = { "lat", "lng" };

    ArrayList list_of_geopoints = new ArrayList();

    HttpResponse response = null;

    try {

        HttpClient httpClient = new DefaultHttpClient();

        HttpContext localContext = new BasicHttpContext();

        HttpPost httpPost = new HttpPost(url);

        response = httpClient.execute(httpPost, localContext);

        InputStream in = response.getEntity().getContent();

        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        Document doc = builder.parse(in);

        if (doc != null) {

            NodeList nl1, nl2;

            nl1 = doc.getElementsByTagName(tag[0]);
            nl2 = doc.getElementsByTagName(tag[1]);

            if (nl1.getLength() > 0) {

                list_of_geopoints = new ArrayList();

                for (int i = 0; i < nl1.getLength(); i++) {

                    Node node1 = nl1.item(i);

                    Node node2 = nl2.item(i);

                    double lat = Double.parseDouble(node1.getTextContent());

                    double lng = Double.parseDouble(node2.getTextContent());

                    list_of_geopoints.add(new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)));

                }

            } else {
                // No points found
            }

        }

    } catch (Exception e) {

        e.printStackTrace();

    }

    return list_of_geopoints;

}

public class MyOverlay extends Overlay {


    private ArrayList all_geo_points;

    public MyOverlay(ArrayList allGeoPoints) {

        super();

      this.all_geo_points = allGeoPoints;

    }


    @Override

    public boolean draw(Canvas canvas, MapView mv, boolean shadow, long when) {

        super.draw(canvas, mv, shadow);

        drawPath(mv, canvas);

        return true;

    }

    public void drawPath(MapView mv, Canvas canvas) {

        int xPrev = -1, yPrev = -1, xNow = -1, yNow = -1;

        Paint paint = new Paint();

        paint.setColor(Color.BLUE);

        paint.setStyle(Paint.Style.FILL_AND_STROKE);

        paint.setStrokeWidth(4);

    paint.setAlpha(100);

        if (all_geo_points != null)

            for (int i = 0; i < all_geo_points.size() - 4; i++) {

                GeoPoint gp = (GeoPoint) all_geo_points.get(i);

                Point point = new Point();

                mv.getProjection().toPixels(gp, point);

                xNow = point.x;

                yNow = point.y;

                if (xPrev != -1) {

                    canvas.drawLine(xPrev, yPrev, xNow, yNow, paint);

               }

                xPrev = xNow;

                yPrev = yNow;

            }

    }

}

}

这是关于谷歌地图的代码是线驱动,因此输入点纬度和经度两个点作为驱动器的路线,输入是数组。在代码中不要出错,但代码有关于数组 8 的警告,警告它关于数组的所有警告。运行程序有关于数组索引的错误 logcat。


01-18 00:37:29.720: E/AndroidRuntime(667): 致命异常: main 01-18 00:37:29.720: E/AndroidRuntime(667): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. example.getgoogledirection/com.example.getgoogledirection.MainActivity}:java.lang.IndexOutOfBoundsException:无效索引 0,大小为 0 01-18 00:37:29.720:E/AndroidRuntime(667):在 android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2059) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 01-18 00:37:29.720: E/AndroidRuntime (667): 在 android.app.ActivityThread.access$600(ActivityThread.java:130) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java :1195) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.os.Handler。dispatchMessage(Handler.java:99) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.os.Looper.loop(Looper.java:137) 01-18 00:37:29.720: E/ AndroidRuntime(667): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 java.lang.reflect.Method.invoke(Method.java:511) 01-18 00:37:29.720: E/AndroidRuntime(667): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-18 00:37:29.720: E/AndroidRuntime(667): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:553) 01-18 00:37:29.720: E/AndroidRuntime(667): at dalvik.system.NativeStart.main(Native Method) 01-18 00:37:29.720: E/AndroidRuntime(667): Caused由:java.lang.IndexOutOfBoundsException:无效索引 0,大小为 0 01-18 00:37:29.720: E/AndroidRuntime(667): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 01-18 00:37:29.720: E/AndroidRuntime(667):在 java.util.ArrayList.get(ArrayList.java:304) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 com.example.getgoogledirection.MainActivity.onCreate(MainActivity.java:45) 01- 18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.Activity.performCreate(Activity.java:5008) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)在 java.util.ArrayList.get(ArrayList.java:304) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 com.example.getgoogledirection.MainActivity.onCreate(MainActivity.java:45) 01- 18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.Activity.performCreate(Activity.java:5008) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)在 java.util.ArrayList.get(ArrayList.java:304) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 com.example.getgoogledirection.MainActivity.onCreate(MainActivity.java:45) 01- 18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.Activity.performCreate(Activity.java:5008) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app。 Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)callActivityOnCreate(Instrumentation.java:1079) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)callActivityOnCreate(Instrumentation.java:1079) 01-18 00:37:29.720: E/AndroidRuntime(667): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

4

1 回答 1

0

如果你把 logcat 的输出放在这里,帮助你会容易得多。正如您所提到的,错误可能来自方法 getDirections。

于 2013-01-17T17:27:01.297 回答