プログラムはエラーではありませんが、インデックス配列リストに関する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;
}
}
}
}
これはコードです グーグルマップについてはラインドライブなので、ドライバーへのウェイラインのポイント緯度と経度の2つのポイントを入力し、入力は配列です。コードではエラーは発生しませんが、コードには配列 8 に関する警告があり、配列に関するすべての警告が表示されます。プログラムを実行すると、配列インデックスに関するエラー logcat が発生します。
01-18 00:37:29.720: E/AndroidRuntime(667): 致命的な例外: メイン 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(ネイティブ メソッド) で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): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 01-18 00:37:29.720: E/AndroidRuntime(667): com.android.internal.os.ZygoteInit.main(ZygoteInit) で.java:553) 01-18 00:37:29.720: E/AndroidRuntime(667): dalvik.system.NativeStart.main(ネイティブ メソッド) 01-18 00:37:29.720: E/AndroidRuntime(667): 原因by: java.lang.IndexOutOfBoundsException: 無効なインデックス 0、サイズは0です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) で