-1

I am saving LatLng coordinates. Method looks like this:

@Override
protected void onPause() {
    markerList.clear();
    sharedPreferences=getPreferences(Context.MODE_PRIVATE);
    editor=sharedPreferences.edit();
    key =0;
    for (LatLng latlng:markerList){
        double lat = latlng.latitude;
        double lng = latlng.longitude;
        editor.putString("key"+key,lat+","+lng);
        key++;
    }
    editor.putInt("id",key);
    editor.apply();

    super.onPause();
}

When i try to get strings and parse to double:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    markerList = new ArrayList<>();
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    sharedPreferences=getPreferences(Context.MODE_PRIVATE);
    try{
        key = sharedPreferences.getInt("id",0);
        for (int i =0;i<=key;i++){
            if (sharedPreferences!=null){
                String toSplit = sharedPreferences.getString("key"+i, null);
                String[] split = toSplit.split(",");
                double lat = Double.parseDouble(split[0]);
                double lng = Double.parseDouble(split[1]);
                LatLng latLng = new LatLng(lat,lng);
                markerList.add(latLng);


            }

        }
    }catch (NullPointerException e){
        e.printStackTrace();
    }catch (NumberFormatException n){
        n.printStackTrace();
    }

i get:

java.lang.NumberFormatException: Invalid double: "lat/lng: (13.182902096722074"

at this line:

double lat = Double.parseDouble(split[0]);

I've went over the code 1000 times and everything looks ok to me? What am i missing?

fixxxera
  • 205
  • 2
  • 10
  • Can you show us what `String toSplit = sharedPreferences.getString("key"+i, null);` returns? – Mauker Dec 18 '15 at 15:17

2 Answers2

1

You're not using a good format when saving those numbers. (Or at least looks like it from your logcat).

I believe you want, and are trying to save something like:

"13.182902096722074,14.182902096722074"

But instead, you have something like this format:

"lat/lng: (13.182902096722074,14.182902096722074)"

You don't have to put that "lat/lng: part when you save the data on the SharedPreferences.


EDIT: From what you've answered. It seems that you were saving the preferences on that format before, and changed your code later. But either forgot to rerun your code, or you were reading an old SharedPreferences file.

Mauker
  • 11,237
  • 7
  • 58
  • 76
0

From the error message, it looks like you're reading a different format than you're currently saving. Did you previously save data in the format "lat/lng: (lat,lng)" instead of your current format "lat,lng"? If so, you need to clear that data.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26