Pergunta

I am getting latitude and longitude values in activity, I am getting these both are String values here I have to cover double and using this latitude and longitude value I want to show locations on map. But while converting String to double it shows error NumberFormatException in my logcat. But I converted using Double.parseDouble("");

My code:

@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"));

    }
    }

Error 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)

How to avoid this ?

Foi útil?

Solução 2

Normally Whenever you are trying to over a Number String ( "1234" ) into Number ( 1234 ), at that time if there is any white space in the number then it throws, NumberFormatException. To avoid it always use trim() method.

Instead of these lines,

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

Try this,

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

Outras dicas

You must check your lat and lng values are NULL or not like:

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

Exception is thrown because your lat and lng values found NULL or blank and you convert that NULL value into Double, so.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top