简体   繁体   English

Android-marker.setIcon()的空引用

[英]Android - Null Reference for marker.setIcon()

EDIT : someone tagged it as a duplicate, but it's quite clear that this is not the case, but mostly an API bug. 编辑 :有人将其标记为重复项,但很明显,事实并非如此,但主要是API错误。

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: 在使用旧版API的手机上测试代码,一切都可以正常工作,但是使用具有API v23的仿真器时,它会崩溃并显示以下日志消息:

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;
}

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 https://developers.google.com/maps/documentation/android-api/marker

I've found a temporary solution, waiting for Google to fix this bug. 我找到了一个临时解决方案,等待Google修复此错误。

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+. 我知道这不是很好,但目前,它是Android 5.0+的唯一解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM