64

I'm trying to get a marker with an custom icon in Google Maps Android API v2. I just changed one of the examples Google provides. I added .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)) to RawMapViewDemoActivity in method setUpMap().

private void setUpMapIfNeeded() {
    if (mMap == null) {
        mMap = ((MapView) findViewById(R.id.map)).getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions()
        .position(new LatLng(0, 0))
        .title("Marker")
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
}

But I'm always getting an "IBitmapDescriptorFactory is not initialized".

12-18 15:40:54.356: E/AndroidRuntime(12591): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.RawMapViewDemoActivity}: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.os.Looper.loop(Looper.java:137)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at java.lang.reflect.Method.invokeNative(Native Method)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at java.lang.reflect.Method.invoke(Method.java:511)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at dalvik.system.NativeStart.main(Native Method)
12-18 15:40:54.356: E/AndroidRuntime(12591): Caused by: java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.google.android.gms.internal.at.a(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.google.android.gms.maps.model.BitmapDescriptorFactory.d(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromResource(Unknown Source)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.example.mapdemo.RawMapViewDemoActivity.setUpMap(RawMapViewDemoActivity.java:67)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.example.mapdemo.RawMapViewDemoActivity.setUpMapIfNeeded(RawMapViewDemoActivity.java:58)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at com.example.mapdemo.RawMapViewDemoActivity.onCreate(RawMapViewDemoActivity.java:43)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.Activity.performCreate(Activity.java:5008)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 15:40:54.356: E/AndroidRuntime(12591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 15:40:54.356: E/AndroidRuntime(12591):    ... 11 more

In BitmapDescriptorFactory it says:

Prior to using any methods from this class, you must do one of the following to ensure that this class is initialized:

  • Wait for a GoogleMap to become available from a MapFragment or MapView that you have added to your application. You can verify that the GoogleMap is available by calling the getMap() method and checking that the returned object is not null.

  • Call MapsInitializer.initialize(Context). As long as a com.google.android.gms.common.GooglePlayServicesNotAvailableException GooglePlayServicesNotAvailableException isn't thrown, this class will be correctly initialized.

I did the first one, but still getting this error. Any suggestions?

Community
  • 1
  • 1
Kris
  • 4,595
  • 7
  • 32
  • 50
  • I suggest creating a sample project that can reproduce the error and posting its source code somewhere. This feels like a bug in `fromResource()`, but we'd need a reproducible test case. – CommonsWare Dec 18 '12 at 17:21
  • 1
    Calling `MapInitializer.initialize()` does the trick for me. – Ash Dec 18 '12 at 22:43
  • 1
    i am facing the same problem..java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized – sanjay Dec 19 '12 at 09:58
  • my sample java class:-http://pastebin.com/Mh7R0WQV – sanjay Dec 19 '12 at 10:05
  • The same error "IBitmapDescriptorFactory is not initialized" happens if I use the defaultMarker() method instead of the fromResource(). `.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)` – Kris Dec 19 '12 at 12:02
  • I've got same message when no Google account was on phone. So, you can try to check registered account on device. – Majestic Jul 22 '16 at 13:21

7 Answers7

112

Call MapsInitializer.initialize(getApplicationContext()) in the onCreate()

Marina.Eariel
  • 163
  • 2
  • 9
Greeny
  • 1,931
  • 2
  • 17
  • 26
  • @Greeny It throws an Exception – Pratik Butani Jul 27 '13 at 12:04
  • 7
    This is a good answer, however it would be great to find out why that really happens in the first place, it's not documented in the maps tutorial AFAIK – avalancha Jan 30 '14 at 07:46
  • 23
    My application still crashes on some phones with this exception even though I have added this line of code. I found it may have something todo with GooglePlay store not installed or up to date. Any way to find out if Maps is available? – X.X_Mass_Developer Mar 31 '14 at 11:00
  • 4
    I am try this but same error nothing change in error – Lazy Lion Aug 30 '14 at 06:19
  • If you have the map inside a fragment please move that line to the fragment activity onCreate() – Francisco Castro Jan 03 '18 at 19:40
  • 1
    The right answer is from @Charlie, you should move your code to be called at 'onMapReady()'. – Mário Augusto Dec 16 '18 at 23:51
  • 1
    Even though this is a solution, it's not a good one since it doesn't work on all android devices. The best solution is to call/use this class after your Map is loaded and ready. Once the map is loaded and ready, a callback is received via the `onMapReady()` method, as mentioned by @Charlie in an answer below. – Edward Quixote Feb 27 '19 at 14:00
24

Move your code to be called onMapReady(), a callback provided by the GoogleMaps API.

Charlie
  • 2,876
  • 19
  • 26
23

Call following in onCreate()

try {
        MapsInitializer.initialize(getApplicationContext());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
1

This happened to me and finally In my case what I could find was the google play services were not installed. So installed them and app did not crash. So may be one can put try and a dialog saying so in the catch block.

0

See Custom marker in google maps in android with vector asset icon.

In my case I had markers over Google Maps. But on API 19 emulator there is a problem with Google Play Services. So this error is misleading, Google Play Services tried to create BitmapDescriptor, but could not.

You should create markers in onMapReady(), also move there any other code, working with Google Maps.

If you have problems with showing Google Maps on Android 4, maybe you should downgrade a library from 16.1.0 to 16.0.0, see https://stackoverflow.com/a/54772374/2914140. But if you do so, you will get a crash on Androif 9, see Google Maps crashing on Android Pie.

CoolMind
  • 26,736
  • 15
  • 188
  • 224
0

Ok here is a solution that's work for me, i found it accidentally ! just upgrade your Gradle to com.android.tools.build:gradle:3.6.2

    dependencies {
    classpath 'com.android.tools.build:gradle:3.6.2'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
Mubashir Murtaza
  • 327
  • 2
  • 14
-1
  GoogleApiAvailability googleApiAvailability=GoogleApiAvailability.getInstance();

  int status=googleApiAvailability.isGooglePlayServicesAvailable(getActivity());



  if (status != ConnectionResult.SUCCESS) {
        int requestCode = 10;
        Dialog dialog = googleApiAvailability.getErrorDialog(getActivity(),status,requestCode);
        dialog.show();
    }else {}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Dmarp
  • 208
  • 1
  • 3
  • 13