From f8be196656bfa3325dbae07c02b8cbd3779c14d6 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 25 Sep 2018 15:53:26 +0300 Subject: [PATCH] Add check for npe and NotFoundException --- .../MapContextMenuFragment.java | 42 ++++++++++++------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java index 04e0c16567..20baed7472 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/MapContextMenuFragment.java @@ -1287,12 +1287,18 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo private void createTransportBadges() { if (!transportBadgesCreated) { - List localTransportStopRoutes = filterTransportRoutes(menu.getLocalTransportStopRoutes()); - List nearbyTransportStopRoutes = filterTransportRoutes(menu.getNearbyTransportStopRoutes()); + List localTransportStopRoutes = menu.getLocalTransportStopRoutes(); + List nearbyTransportStopRoutes = menu.getNearbyTransportStopRoutes(); int localColumnsPerRow = getRoutesBadgesColumnsPerRow(null); - int maxLocalRows = (int) Math.round(Math.ceil((double) localTransportStopRoutes.size() / localColumnsPerRow)); - updateLocalRoutesBadges(localTransportStopRoutes, localColumnsPerRow); - updateNearbyRoutesBadges(maxLocalRows, nearbyTransportStopRoutes); + int maxLocalRows = 0; + if (localTransportStopRoutes != null) { + List localFilteredTransportStopRoutes = filterTransportRoutes(localTransportStopRoutes); + maxLocalRows = (int) Math.round(Math.ceil((double) localFilteredTransportStopRoutes.size() / localColumnsPerRow)); + updateLocalRoutesBadges(localFilteredTransportStopRoutes, localColumnsPerRow); + } + if (nearbyTransportStopRoutes != null) { + updateNearbyRoutesBadges(maxLocalRows, filterTransportRoutes(nearbyTransportStopRoutes)); + } transportBadgesCreated = true; } } @@ -1341,18 +1347,22 @@ public class MapContextMenuFragment extends BaseOsmAndFragment implements Downlo } private int getRoutesBadgesColumnsPerRow(@Nullable String nearInDistance) { - double badgeWidth = getResources().getDimension(R.dimen.context_menu_transport_grid_item_width); - double gridSpacing = getResources().getDimension(R.dimen.context_menu_transport_grid_spacing); - double gridPadding = getResources().getDimension(R.dimen.context_menu_padding_margin_default); - int availableSpace; - if (nearInDistance == null) { - availableSpace = (int) (routesBadgesContainer.getWidth() - gridPadding * 2); - } else { - int textWidth = AndroidUtils.getTextWidth(getResources().getDimensionPixelSize(R.dimen.default_sub_text_size), nearInDistance); - double paddingTv = getResources().getDimension(R.dimen.context_menu_padding_margin_small); - availableSpace = (int) (routesBadgesContainer.getWidth() - gridPadding * 2 - paddingTv - textWidth); + try { + double badgeWidth = getResources().getDimension(R.dimen.context_menu_transport_grid_item_width); + double gridSpacing = getResources().getDimension(R.dimen.context_menu_transport_grid_spacing); + double gridPadding = getResources().getDimension(R.dimen.context_menu_padding_margin_default); + int availableSpace; + if (nearInDistance == null) { + availableSpace = (int) (routesBadgesContainer.getWidth() - gridPadding * 2); + } else { + int textWidth = AndroidUtils.getTextWidth(getResources().getDimensionPixelSize(R.dimen.default_sub_text_size), nearInDistance); + double paddingTv = getResources().getDimension(R.dimen.context_menu_padding_margin_small); + availableSpace = (int) (routesBadgesContainer.getWidth() - gridPadding * 2 - paddingTv - textWidth); + } + return (int) ((availableSpace + gridSpacing) / (badgeWidth + gridSpacing)); + } catch (Resources.NotFoundException e) { + return -1; } - return (int) ((availableSpace + gridSpacing) / (badgeWidth + gridSpacing)); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN)