Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java.lang.IndexOutOfBoundsException:索引0无效,大小为0_Android_Google Maps - Fatal编程技术网

Android java.lang.IndexOutOfBoundsException:索引0无效,大小为0

Android java.lang.IndexOutOfBoundsException:索引0无效,大小为0,android,google-maps,Android,Google Maps,这是我的代码和日志 public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.setOnMapClickListener(this); mMap.setMyLocationEnabled(true); mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); mMap.setMyLocationEnabled(true); mLocationRequest = new

这是我的代码和日志

public void onMapReady(GoogleMap googleMap) {

 mMap = googleMap;

 mMap.setOnMapClickListener(this);

 mMap.setMyLocationEnabled(true);

mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

 mMap.setMyLocationEnabled(true);

mLocationRequest = new LocationRequest();

 mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

 mLocationRequest.setInterval(2000);

 mLocationRequest.setFastestInterval(1000);

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

 String provider = locationManager.getBestProvider(new Criteria(), true);

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling

        return;
    }
    Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (location != null) {
        Log.e("TAG", "GPS is on");
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Toast.makeText(getApplicationContext()
                , "latitude:" + latitude + " longitude:" + longitude, Toast.LENGTH_SHORT).show();

    } else {

        locationManager.requestLocationUpdates(provider, 4000, 0,  this);
    }
    LatLng HYDERABAD = new LatLng(latitude, longitude);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HYDERABAD, 12));
    try {
        List<Address> addresses;
        Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.ENGLISH);
        addresses = geocoder.getFromLocation(latitude, longitude, 1);

        if (Geocoder.isPresent()) {
            Toast.makeText(getApplicationContext(), "geocoder present",
                    Toast.LENGTH_SHORT).show();

          Address returnAddress = addresses.get(0);
           Log.d("LIne ", returnAddress.toString());
            String localityString = returnAddress.getAddressLine(2);
           Log.d("millatary ", localityString);

            str.append(localityString).append(" ");

            marker = new MarkerOptions().position(
                    new LatLng(latitude, longitude)).title(
                    str.toString());
            etOrigin.setText(str.toString());

            mCurrLocationMarker = mMap.addMarker(marker);
            Toast.makeText(getApplicationContext(), str,
                    Toast.LENGTH_SHORT).show();
        } else
            Toast.makeText(getApplicationContext(),
                    "geocoder not present", Toast.LENGTH_SHORT).show();

    } catch (IOException e) {
        Toast.makeText(getApplicationContext(), "Exception",
                Toast.LENGTH_SHORT).show();
    }
From of
Geocoder.getFromLocation)(

返回

列出地址对象的列表如果未找到匹配项或没有可用的后端服务,则返回null或空列表。

在您的示例中,它返回一个空的
列表
,您必须检查这种情况:

if (addresses != null && !addresses.isEmpty()) {
    // Your code:
    Address returnAddress = addresses.get(0);
    Log.d("LIne ", returnAddress.toString());
    String localityString = returnAddress.getAddressLine(2);
    Log.d("millatary ", localityString);

    str.append(localityString).append(" ");

    marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title(
                str.toString());
    etOrigin.setText(str.toString());

    mCurrLocationMarker = mMap.addMarker(marker);
    Toast.makeText(getApplicationContext(), str,
        Toast.LENGTH_SHORT).show();
}
if (addresses != null && !addresses.isEmpty()) {
    // Your code:
    Address returnAddress = addresses.get(0);
    Log.d("LIne ", returnAddress.toString());
    String localityString = returnAddress.getAddressLine(2);
    Log.d("millatary ", localityString);

    str.append(localityString).append(" ");

    marker = new MarkerOptions().position(
        new LatLng(latitude, longitude)).title(
                str.toString());
    etOrigin.setText(str.toString());

    mCurrLocationMarker = mMap.addMarker(marker);
    Toast.makeText(getApplicationContext(), str,
        Toast.LENGTH_SHORT).show();
}