Fix possible NPE

This commit is contained in:
Alexander Sytnyk 2017-08-01 15:16:42 +03:00
parent 1f9a7518d5
commit 3f742be6ec

View file

@ -19,7 +19,6 @@ public class MeasurementToolFragment extends Fragment {
public static final String TAG = "MeasurementToolFragment"; public static final String TAG = "MeasurementToolFragment";
private MapActivity mapActivity;
private MeasurementToolLayer measurementLayer; private MeasurementToolLayer measurementLayer;
private TextView distanceTv; private TextView distanceTv;
@ -31,7 +30,7 @@ public class MeasurementToolFragment extends Fragment {
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mapActivity = (MapActivity) getActivity(); MapActivity mapActivity = (MapActivity) getActivity();
measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer(); measurementLayer = mapActivity.getMapLayers().getMeasurementToolLayer();
IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache(); IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache();
final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
@ -75,51 +74,64 @@ public class MeasurementToolFragment extends Fragment {
exitMeasurementMode(); exitMeasurementMode();
} }
private MapActivity getMapActivity() {
return (MapActivity) getActivity();
}
private void updateText() { private void updateText() {
distanceTv.setText(measurementLayer.getDistanceSt() + ","); distanceTv.setText(measurementLayer.getDistanceSt() + ",");
pointsTv.setText(pointsSt + ": " + measurementLayer.getPointsCount()); pointsTv.setText(pointsSt + ": " + measurementLayer.getPointsCount());
} }
private void enterMeasurementMode() { private void enterMeasurementMode() {
measurementLayer.setInMeasurementMode(true); MapActivity mapActivity = getMapActivity();
mapActivity.refreshMap(); if (mapActivity != null) {
mapActivity.disableDrawer(); measurementLayer.setInMeasurementMode(true);
mark(View.INVISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info); mapActivity.refreshMap();
mark(View.GONE, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, mapActivity.disableDrawer();
R.id.map_search_button, R.id.map_quick_actions_button); mark(View.INVISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info);
mark(View.GONE, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button,
R.id.map_search_button, R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button); View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && collapseButton.getVisibility() == View.VISIBLE) { if (collapseButton != null && collapseButton.getVisibility() == View.VISIBLE) {
wasCollapseButtonVisible = true; wasCollapseButtonVisible = true;
collapseButton.setVisibility(View.INVISIBLE); collapseButton.setVisibility(View.INVISIBLE);
} else { } else {
wasCollapseButtonVisible = false; wasCollapseButtonVisible = false;
}
updateText();
} }
updateText();
} }
private void exitMeasurementMode() { private void exitMeasurementMode() {
measurementLayer.setInMeasurementMode(false); MapActivity mapActivity = getMapActivity();
mapActivity.refreshMap(); if (mapActivity != null) {
mapActivity.enableDrawer(); measurementLayer.setInMeasurementMode(false);
mark(View.VISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info, mapActivity.refreshMap();
R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, mapActivity.enableDrawer();
R.id.map_search_button, R.id.map_quick_actions_button); mark(View.VISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info,
R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button,
R.id.map_search_button, R.id.map_quick_actions_button);
View collapseButton = mapActivity.findViewById(R.id.map_collapse_button); View collapseButton = mapActivity.findViewById(R.id.map_collapse_button);
if (collapseButton != null && wasCollapseButtonVisible) { if (collapseButton != null && wasCollapseButtonVisible) {
collapseButton.setVisibility(View.VISIBLE); collapseButton.setVisibility(View.VISIBLE);
}
measurementLayer.clearPoints();
} }
measurementLayer.clearPoints();
} }
private void mark(int status, int... widgets) { private void mark(int status, int... widgets) {
for (int widget : widgets) { MapActivity mapActivity = getMapActivity();
View v = mapActivity.findViewById(widget); if (mapActivity != null) {
if (v != null) { for (int widget : widgets) {
v.setVisibility(status); View v = mapActivity.findViewById(widget);
if (v != null) {
v.setVisibility(status);
}
} }
} }
} }