0

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;
}
Davide3i
  • 1,035
  • 3
  • 15
  • 35
  • @Rotwang, please read all my question and the link I've pointed out before marking it as a duplicate. This is not a good thing by you. – Davide3i Jul 30 '16 at 09:08
  • Just look at link I've posted: there are a lot of other people that have encountered this bug. I was just asking for a workaround. https://code.google.com/p/gmaps-api-issues/issues/detail?id=7696 – Davide3i Jul 30 '16 at 09:10
  • There's a workaround on the same page you linked: `This bug can be avoided by using resources instead of assets. "res/drawable-nodpi" is a viable substitution.` – Phantômaxx Jul 30 '16 at 09:14
  • It's not working in my case, that's why I've asked. I would be glad if you can take off the "duplicate" mark for other being able to help me. It's pretty clear this is not a common issue. Thank you. – Davide3i Jul 30 '16 at 09:17
  • Refresh your browser – Phantômaxx Jul 30 '16 at 09:17
  • can you please show us `getMarkerIcon` method content – younes zeboudj Jul 30 '16 at 13:47
  • i think it is a problem with drawables, may be you have a drawable with a specific selector, for example drawable-v21 and you don't have a symetric one in geniric folder `drawable`, so when using v23 the system will not find a corresponding drawable – younes zeboudj Jul 30 '16 at 14:02

2 Answers2

1

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

ViramP
  • 1,659
  • 11
  • 11
  • 1
    That's not the aim of my code: I'm using different colours depending on what kind of fuel the user has selected. To do that I've to set the colour run time, so yep, the library has definitely a problem. Check the link I've posted for further info. – Davide3i Jul 30 '16 at 13:49
  • Sorry man, But i have checked with your code,instead of your drawable i have used my own icon and all working well. check below code may be you can get solution: – ViramP Jul 30 '16 at 14:12
  • public void onMapReady(GoogleMap googleMap) { mMap = googleMap; lat = 65.9667; lang = -18.5333; // Add a marker in Sydney and move the camera LatLng location = new LatLng(lat, lang); // mMap.addMarker(new MarkerOptions().position(location).title(companyname)); for (int i = 1; i <=5; i++) { if (mMap != null) { Marker marker = mMap.addMarker(new MarkerOptions().position(new LatLng(lat+1, lang- 2))); – ViramP Jul 30 '16 at 14:12
  • marker.setIcon(BitmapDescriptorFactory.fromResource(getMarkerIcon(i))); } } mMap.moveCamera(CameraUpdateFactory.newLatLng(location)); //Move the camera to the user's location and zoom in! mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(location, 5.0f)); } – ViramP Jul 30 '16 at 14:12
  • public static int getMarkerIcon(long fuelId) { int drawableId = -1; switch ((int) fuelId) { case 1: { drawableId = R.mipmap.icn_location; break; } case 2: { drawableId = R.mipmap.icn_search_arrow; break; } case 3: { drawableId = R.mipmap.icn_camera; break; } – ViramP Jul 30 '16 at 14:13
  • case 4: { drawableId = R.mipmap.icn_done; break; } case 5: { drawableId = R.mipmap.icn_edit; break; } case 6 : { drawableId = R.mipmap.icn_location_gray; break; } } return drawableId; } – ViramP Jul 30 '16 at 14:13
  • Are you using Vectors? If yes, are you testing it on an Android version greater or equal to 5.0? From your code it looks like you're using png images, not vectors. – Davide3i Jul 30 '16 at 14:56
0

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+.

Davide3i
  • 1,035
  • 3
  • 15
  • 35