android – Google Maps CameraUpdateFactory not initialized-ThrowExceptions

Exception or error:

I’m getting some reports back from user experience through crashlytics giving me an error

Fatal Exception java.lang.NullPointerException         
CameraUpdateFactory is not initialized

This is not a regular crash, as in it doesn’t occur for every user but it is becoming too regular and I need to resolve it.

I had read that this could happen if the maps hadn’t been initialized which I think I’ve covered

if(googleMap!=null){
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(selectedLatLng));
            }  

also a probably cause could be that google play services isn’t on the device or is out of date and I’ve added some verification for that also.

   public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(FuelsFragment.this.getActivity());

    // Showing status
    //CAMERAUPDATE FACTORY CRASH CAN BE A RESULT OF GOOGLE PLAY SERVICES NOT INSTALLED OR OUT OF DATE
    //ADDITIONAL VERIFICATION ADDED TO PREVENT FURTHER CRASHES

    //https://github.com/imhotep/MapKit/pull/17
    if(status == ConnectionResult.SUCCESS)
    {
        mMapFragment = ReepMapFragment.newInstance();

        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.mapContainer, mMapFragment);
        fragmentTransaction.commit();

    }
    else if(status == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED){

        reep.toastNotify("You need to update Google Play Services in order to view maps");
    }
    else if (status==ConnectionResult.SERVICE_MISSING){
        reep.toastNotify("Google Play service is not enabled on this device.");
    }

After that I’m unsure what next to do as this doesn’t happen for each user.

If anyone has any thoughts on why this occurs I’d appreciate your input

How to solve:

I was getting the same error, even though I obtained a non-null GoogleMap object from a MapView. I resolved it with an explicit call to MapsInitializer, even though the documentation says it’s not needed.

My app’s Fragment sets up the MapView via the following:

@Override public View 
onCreateView(LayoutInflater inflater, ViewGroup container,
             Bundle savedInstanceState)
{
    View view = inflater.inflate(R.layout.map_panel, container, false);
    mapView = (MapView) view.findViewById(R.id.map_view);
    mapView.onCreate(savedInstanceState);
    configureMap(mapView.getMap());
    return view;
}

private void 
configureMap(GoogleMap map, double lat, double lon)
{
    if (map == null)
        return; // Google Maps not available
    try {
        MapsInitializer.initialize(getActivity());
    }
    catch (GooglePlayServicesNotAvailableException e) {
        Log.e(LOG_TAG, "Have GoogleMap but then error", e);
        return;
    }
    map.setMyLocationEnabled(true);
    LatLng latLng = new LatLng(lat, lon);
    CameraUpdate camera = CameraUpdateFactory.newLatLng(latLng);
    map.animateCamera(camera);
}

Before I added the call to MapsInitializer, I would get an exception from CameraUpdateFactory. After adding the call, CameraUpdateFactory always succeeds.

###

Just use MapsInitializer.initialize without try-catch, because it doesn’t throw GooglePlayServicesNotAvailableException in the latest version of Google Play services, which was verified with package version 5084032.

###

some time it may be caused by old version of Google Play service please Update Google play service it may helps you

###

May be you should do a Class.forName() ? for CameraUpdateFactory ? in try - catch

###

Something from my MapUtil, that handles this case:

public static void animateCamera(final MapView mapView, final GoogleMap map, final LatLng location) {
        try{
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, getBetterZoom(map));
            map.animateCamera(cameraUpdate);
        }catch(Exception e) {
            MapsInitializer.initialize(mapView.getContext());
            if (mapView.getViewTreeObserver().isAlive()) {
                mapView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                    @SuppressLint("NewApi")
                    @Override
                    public void onGlobalLayout() {
                        GlobalyLayoutListenerUtil.removeGlobalLayoutListener(mapView, this);
                        animateCamera(mapView, mapView.getMap(), location);
                    }
                });
            } else {
                if(map != null){
                    map.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                        @Override
                        public void onMapLoaded() {
                            if(location != null){
                                animateCamera(mapView, mapView.getMap(), location);
                            }
                        }
                    });
                }
            }
        }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *