Reputation: 1053
EDIT: someone tagged it as a duplicate, but it's quite clear that this is not the case, but mostly an API bug.
I've encountered a strange problem using Maps.
First of all my code:
@Override
public void onListUpdated() {
// Cleaning all the markers.
if (mGoogleMap != null) {
mGoogleMap.clear();
}
List<PetrolStation> petrolStationList = mPetrolStationsList.getList();
final HashMap<Marker, PetrolStation> markerPetrolStationHashMap = new HashMap<>();
// Get the fuel selected by user.
String fuelPrefs = loadPreferences(SETTINGS_PREFERENCES, FUEL_KEY);
long fuelId = Long.valueOf(fuelPrefs);
for (PetrolStation petrolStation : petrolStationList) {
double lat = petrolStation.getLat();
double lon = petrolStation.getLon();
if (mGoogleMap != null) {
Marker marker = mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(lat, lon)));
PROBLEM IS HERE -> marker.setIcon(BitmapDescriptorFactory.fromResource(getMarkerIcon(fuelId)));
markerPetrolStationHashMap.put(marker, petrolStation);
}
}
...
}
This is a method of an interface I've declared.
Testing the code on phones with older APIs everything is working fine, but using an emulator with API v23, it crashes with this log message:
java.lang.NullPointerException: null reference
at maps.w.c.a(Unknown Source)
at maps.ad.g$a.<init>(Unknown Source)
at maps.ad.g.a(Unknown Source)
at maps.ad.S.a(Unknown Source)
at abq.onTransact(:com.google.android.gms.DynamiteModulesB:204)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.model.internal.zzf$zza$zza.zzak(Unknown Source)
PROBLEM IS HERE -> at com.google.android.gms.maps.model.Marker.setIcon(Unknown Source)
at com.myfuel.fragments.MapFragment.onListUpdated(MapFragment.java:317)
at com.myfuel.utils.PetrolStationsList$PetrolStationsAsyncTask.onPostExecute(PetrolStationsList.java:229)
at com.myfuel.utils.PetrolStationsList$PetrolStationsAsyncTask.onPostExecute(PetrolStationsList.java:151)
...
I've pointed out the line causing the crash in my code and the reference inside of the log.
Reading around I've found out this is probably a bug (not fixed after all this time), but it's quite annoying and I hope someone will point me out some kind of solution.
Thank you.
EDIT:
public static int getMarkerIcon(long fuelId) {
int drawableId = -1;
switch ((int) fuelId) {
case 1: {
drawableId = R.drawable.marker_petrol;
break;
}
case 2: {
drawableId = R.drawable.marker_special_petrol;
break;
}
case 3: {
drawableId = R.drawable.marker_lpg;
break;
}
case 4: {
drawableId = R.drawable.marker_diesel_image;
break;
}
case 5: {
drawableId = R.drawable.marker_special_diesel;
break;
}
case 6 : {
drawableId = R.drawable.marker_methane;
break;
}
}
return drawableId;
}
Upvotes: 0
Views: 1031
Reputation: 1709
Library have no issue.
Please try below code snippet.
static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
https://developers.google.com/maps/documentation/android-api/marker
Upvotes: 1
Reputation: 1053
I've found a temporary solution, waiting for Google to fix this bug.
Just use Images instead of Vectors. I know it's not that nice but, for now, it's the only solution for Android 5.0+.
Upvotes: 0