diff options
| -rw-r--r-- | main/src/cgeo/geocaching/maps/MapProviderFactory.java | 15 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/settings/SettingsActivity.java | 14 |
2 files changed, 28 insertions, 1 deletions
diff --git a/main/src/cgeo/geocaching/maps/MapProviderFactory.java b/main/src/cgeo/geocaching/maps/MapProviderFactory.java index 84c9329..a03fc47 100644 --- a/main/src/cgeo/geocaching/maps/MapProviderFactory.java +++ b/main/src/cgeo/geocaching/maps/MapProviderFactory.java @@ -69,6 +69,12 @@ public class MapProviderFactory { parentMenu.setGroupCheckable(R.id.menu_group_map_sources, true, true); } + /** + * Return a map source by id. + * + * @param id the map source id + * @return the map source, or <tt>null</tt> if <tt>id</tt> does not correspond to a registered map source + */ public static MapSource getMapSource(int id) { for (MapSource mapSource : mapSources) { if (mapSource.getNumericalId() == id) { @@ -78,6 +84,15 @@ public class MapProviderFactory { return null; } + /** + * Return a map source if there is at least one. + * + * @return the first map source in the collection, or <tt>null</tt> if there are none registered + */ + public static MapSource getAnyMapSource() { + return mapSources.isEmpty() ? null : mapSources.get(0); + } + public static void registerMapSource(final MapSource mapSource) { mapSources.add(mapSource); } diff --git a/main/src/cgeo/geocaching/settings/SettingsActivity.java b/main/src/cgeo/geocaching/settings/SettingsActivity.java index 42f6074..ece2acb 100644 --- a/main/src/cgeo/geocaching/settings/SettingsActivity.java +++ b/main/src/cgeo/geocaching/settings/SettingsActivity.java @@ -40,6 +40,7 @@ import android.view.View; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ListAdapter; +import android.widget.Toast; import java.io.File; import java.util.ArrayList; @@ -482,7 +483,18 @@ public class SettingsActivity extends PreferenceActivity { } else if (isPreference(preference, R.string.pref_mapsource)) { // reset the cached map source int mapSourceId = Integer.valueOf(stringValue); - final MapSource mapSource = MapProviderFactory.getMapSource(mapSourceId); + MapSource mapSource = MapProviderFactory.getMapSource(mapSourceId); + // If there is no corresponding map source (because some map sources were + // removed from the device since) then use the first one available. + if (mapSource == null) { + mapSource = MapProviderFactory.getAnyMapSource(); + if (mapSource == null) { + // There are no map source. There is little we can do here, except log an error and + // return to avoid triggering a null pointer exception. + Log.e("SettingsActivity.onPreferenceChange: no map source available"); + return true; + } + } Settings.setMapSource(mapSource); preference.setSummary(mapSource.getName()); } else if (isPreference(preference, R.string.pref_connectorOCActive) || isPreference(preference, R.string.pref_connectorOCPLActive) || isPreference(preference, R.string.pref_connectorGCActive)) { |
