緯度と経度を渡して位置を取得するために Geocoder を使用しようとしています。しかし、私は常にゼロの配列サイズとエラーを取得しますindexoutofboundexception index 0 size 0
。
経度と緯度の値を変えてみました。
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if (gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
List<Address> address;
Geocoder gcd = new Geocoder(getBaseContext(), Locale
.getDefault());
try {
address = gcd.getFromLocation(latitude, longitude, 1);
if (address != null && address.size() >= 0) {
loc = address.get(0).getAdminArea();
Toast.makeText(
getApplicationContext(),
"Your Location is - \nLat: " + latitude
+ "\nLong: " + longitude + "\nLocation: "
+ loc, Toast.LENGTH_LONG).show();
} else
Toast.makeText(getApplicationContext(), "no data",
Toast.LENGTH_LONG).show();
// System.out.println("size of ade " + address.size());
// System.out.println(address.get(0).getLocality());
// loc = address.get(0).getLocality();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// \n is for new line
// Toast.makeText(
// getApplicationContext(),
// "Your Location is - \nLat: " + latitude
// + "\nLong: " + longitude + "\nLocation: "
// + loc, Toast.LENGTH_LONG).show();
} else {
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
ここにスタックトレースがあります
03-26 14:01:06.592: E/AndroidRuntime(11006): FATAL EXCEPTION: main
03-26 14:01:06.592: E/AndroidRuntime(11006): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-26 14:01:06.592: E/AndroidRuntime(11006): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-26 14:01:06.592: E/AndroidRuntime(11006): at java.util.ArrayList.get(ArrayList.java:304)
03-26 14:01:06.592: E/AndroidRuntime(11006): at com.example.gpstracker.MainActivity$1.onClick(MainActivity.java:54)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.view.View.performClick(View.java:4204)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.view.View$PerformClick.run(View.java:17355)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.os.Handler.handleCallback(Handler.java:725)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.os.Looper.loop(Looper.java:137)
03-26 14:01:06.592: E/AndroidRuntime(11006): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-26 14:01:06.592: E/AndroidRuntime(11006): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 14:01:06.592: E/AndroidRuntime(11006): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 14:01:06.592: E/AndroidRuntime(11006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-26 14:01:06.592: E/AndroidRuntime(11006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-26 14:01:06.592: E/AndroidRuntime(11006): at dalvik.system.NativeStart.main(Native Method)
私の間違いは何ですか?ありがとう。