0

i create location-based application. within the application there is a method to get user position. Some of users encountered that the app crash when trying to get user position. below is my get user position method.

// Method to get user position
public void getUserPosition(double latitude, double longitude){
    // Check distance between user position and default position
    Location.distanceBetween(latitude, longitude,
            mUtils.ARG_DEFAULT_LATITUDE, mUtils.ARG_DEFAULT_LONGITUDE, mCheckDistances);

    // If the distance is more than maximum distance, then use default position
    if (Double.valueOf(String.format("%.2f", (mCheckDistances[0] / 1000))) > mUtils.ARG_MAX_DISTANCE) {
        mCurrentLocation = new Location("");
        mCurrentLocation.setLatitude(mUtils.ARG_DEFAULT_LATITUDE);
        mCurrentLocation.setLongitude(mUtils.ARG_DEFAULT_LONGITUDE);
        mCurrentLatitude = mCurrentLocation.getLatitude();
        mCurrentLongitude = mCurrentLocation.getLongitude();

        if(mLocationResultStatus == Activity.RESULT_CANCELED){
            showSnackbar(getString(R.string.gps_not_enabled_alert));
        }else {
            showSnackbar(getString(R.string.distance_alert));
        }

    }else{
        mCurrentLatitude = latitude;
        mCurrentLongitude = longitude;
    }

    // Move camera to user position
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
            new LatLng(mCurrentLatitude, mCurrentLongitude),
            mUtils.ARG_DEFAULT_MAP_ZOOM_LEVEL);
    mMap.animateCamera(cameraUpdate);

}

and below is the error log,

java.lang.NumberFormatException: Invalid double: "12550,25"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at java.lang.Double.valueOf(Double.java:338)
at com.pongodev.locazee.activities.ActivityHome.getUserPosition(ActivityHome.java:675)
at com.pongodev.locazee.activities.ActivityHome.onLocationChanged(ActivityHome.java:1031)
at com.google.android.gms.location.internal.zzi$zzb.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

The error log says that Invalid double but the result of Location.distanceBetween is float. is there anyone can help me to fix this out? many thanks.

taufan
  • 219
  • 1
  • 2
  • 12

3 Answers3

0

You can cast the float to a double by doing (double) floatName but this will cause round-off error so don't expect precision.

LonelyIdiot
  • 789
  • 2
  • 6
  • 16
0

The actual error is with your value : 12550,25 the value should be like 12550.25 not 12550,25.

If you want to convert any string to double, that string should contain only numbers and "." not anything else.

Siri
  • 701
  • 1
  • 6
  • 27
0

This looks like a localization issue when formatting your string. Take a look at this post: DecimalFormat and Double.valueOf()

Community
  • 1
  • 1
WindsurferOak
  • 4,861
  • 1
  • 31
  • 36