Fix possible NPE in the BasicEditPoiFragment

This commit is contained in:
Alex Sytnyk 2018-08-02 12:59:07 +03:00
parent 99c61498bc
commit 998a9f9c99

View file

@ -5,6 +5,7 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable; import android.text.Editable;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.TextWatcher; import android.text.TextWatcher;
@ -31,6 +32,8 @@ import net.osmand.util.OpeningHoursParser.BasicOpeningHourRule;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import java.util.Map;
import gnu.trove.list.array.TIntArrayList; import gnu.trove.list.array.TIntArrayList;
public class BasicEditPoiFragment extends BaseOsmAndFragment public class BasicEditPoiFragment extends BaseOsmAndFragment
@ -123,11 +126,12 @@ public class BasicEditPoiFragment extends BaseOsmAndFragment
@Override @Override
public void afterTextChanged(Editable s) { public void afterTextChanged(Editable s) {
if (!getData().isInEdit()) { EditPoiData data = getData();
if (data != null && !data.isInEdit()) {
if (!TextUtils.isEmpty(s)) { if (!TextUtils.isEmpty(s)) {
getData().putTag(tag, s.toString()); data.putTag(tag, s.toString());
} else { } else {
getData().removeTag(tag); data.removeTag(tag);
} }
} }
} }
@ -145,30 +149,29 @@ public class BasicEditPoiFragment extends BaseOsmAndFragment
mOpeningHoursAdapter.setOpeningHoursRule(item, position); mOpeningHoursAdapter.setOpeningHoursRule(item, position);
} }
private EditPoiDialogFragment getEditPoiFragment() {
return (EditPoiDialogFragment) getParentFragment();
}
private EditPoiData getData() { private EditPoiData getData() {
return getEditPoiFragment().getEditPoiData(); Fragment parent = getParentFragment();
if (parent != null && parent instanceof EditPoiDialogFragment) {
return ((EditPoiDialogFragment) parent).getEditPoiData();
}
return null;
} }
@Override @Override
public void onFragmentActivated() { public void onFragmentActivated() {
streetEditText.setText(getData().getTagValues() EditPoiData data = getData();
.get(OSMSettings.OSMTagKey.ADDR_STREET.getValue())); if (data == null) {
houseNumberEditText.setText(getData().getTagValues() return;
.get(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue())); }
phoneEditText.setText(getData().getTagValues() Map<String, String> tagValues = data.getTagValues();
.get(OSMSettings.OSMTagKey.PHONE.getValue())); streetEditText.setText(tagValues.get(OSMSettings.OSMTagKey.ADDR_STREET.getValue()));
webSiteEditText.setText(getData().getTagValues() houseNumberEditText.setText(tagValues.get(OSMSettings.OSMTagKey.ADDR_HOUSE_NUMBER.getValue()));
.get(OSMSettings.OSMTagKey.WEBSITE.getValue())); phoneEditText.setText(tagValues.get(OSMSettings.OSMTagKey.PHONE.getValue()));
descriptionEditText.setText(getData().getTagValues() webSiteEditText.setText(tagValues.get(OSMSettings.OSMTagKey.WEBSITE.getValue()));
.get(OSMSettings.OSMTagKey.DESCRIPTION.getValue())); descriptionEditText.setText(tagValues.get(OSMSettings.OSMTagKey.DESCRIPTION.getValue()));
OpeningHoursParser.OpeningHours openingHours = OpeningHoursParser.OpeningHours openingHours =
OpeningHoursParser.parseOpenedHoursHandleErrors(getData().getTagValues() OpeningHoursParser.parseOpenedHoursHandleErrors(tagValues
.get(OSMSettings.OSMTagKey.OPENING_HOURS.getValue())); .get(OSMSettings.OSMTagKey.OPENING_HOURS.getValue()));
if (openingHours == null) { if (openingHours == null) {
openingHours = new OpeningHoursParser.OpeningHours(); openingHours = new OpeningHoursParser.OpeningHours();