Lat Lng值中的NumberFormatException

时间:2014-03-04 09:35:43

标签: android google-maps numberformatexception

我在活动中得到纬度和经度值,我得到这两个都是字符串值在这里我必须覆盖双倍并使用这个纬度和经度值我想在地图上显示位置。但是在将String转换为double时,它会在NumberFormatException中显示错误logcat。但是我使用Double.parseDouble("");

转换了

我的代码:

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

    //locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    _googleMap.clear();
    ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, 
        String>>) 
            getIntent().getSerializableExtra("arrayList");
         if(arl.size()!=0){
            for(int j = 0;j<arl.size();j++){


          Log.e("SIZE", ""+arl.size());
           Log.e(" NEW LAT",arl.get(j).get("lat").toString());
          Log.e(" NEW LONG",arl.get(j).get("lng").toString());
          Log.e(" NEW ADDRESS",arl.get(j).get("address").toString());
          Log.e(" NEW CTIME",arl.get(j).get("ctime").toString());
          String lat =arl.get(j).get("lat").toString();
          String lng =arl.get(j).get("lng").toString();
          double Hlat = Double.parseDouble(lat);
          double Hlong= Double.parseDouble(lng);

          LatLng latLng =new LatLng(Hlat, Hlong);

          Marker perth = _googleMap.addMarker(new MarkerOptions()

          .position(latLng)

     .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
          .flat(true));

            }

    }
         else{

    if(location!=null){
        double latitude = location.getLatitude();
        double langitude = location.getLongitude();


        latLongDetails.setLat(latitude);
        latLongDetails.setLongi(langitude);

        Log.e("lat",""+ latLongDetails.getLat());
        Log.e("long", ""+latLongDetails.getLongi());

        LatLng latlang = new LatLng(latitude, langitude);
        //LatLngBounds curScreen = 
           _googleMap.getProjection().getVisibleRegion().latLngBounds;
        //curScreen.contains(latlang);
        myPosition = new LatLng(latitude, langitude);

        _googleMap.moveCamera(CameraUpdateFactory.newLatLng(myPosition)); 
        _googleMap.addMarker(new   
          MarkerOptions().position(myPosition).title("start"));

    }
    }

错误LogCat:

    03-04 14:52:01.765: E/AndroidRuntime(24634): FATAL EXCEPTION: main
03-04 14:52:01.765: E/AndroidRuntime(24634): java.lang.NumberFormatException: 
03-04 14:52:01.765: E/AndroidRuntime(24634):    at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
03-04 14:52:01.765: E/AndroidRuntime(24634):    at java.lang.Double.parseDouble(Double.java:318)
03-04 14:52:01.765: E/AndroidRuntime(24634):    at com.technowellServices.locationfind.GetLatLongForTPActivity.onLocationChanged(GetLatLongForTPActivity.java:161)
03-04 14:52:01.765: E/AndroidRuntime(24634):    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
03-04 14:52:01.765: E/AndroidRuntime(24634):    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
03-04 14:52:01.765: E/AndroidRuntime(24634):    at android.location.L
ocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)

如何避免这种情况?

2 个答案:

答案 0 :(得分:2)

通常情况下,当您尝试将数字字符串(“1234”)转换为数字(1234)时,如果数字中有任何空格,则它会抛出NumberFormatException。为避免它,请始终使用trim()方法。

而不是这些行,

double Hlat = Double.parseDouble(lat);
double Hlong= Double.parseDouble(lng);

试试这个,

if ( !lat.trim().equals("") && !lng.trim().equals("") ) 
{
     double Hlat = Double.parseDouble(lat.trim());
     double Hlong= Double.parseDouble(lng.trim());
}

答案 1 :(得分:2)

您必须检查latlngNULL是否为if (!lat.trim().equals("") && !lng.trim().equals("") ) { double Hlat = Double.parseDouble(lat); double Hlong= Double.parseDouble(lng); }

Exception

lat因为您的lngNULL值找到NULL或空白而您将Double值转换为{{1}},因此