Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps CameraUpdateFactory not initialized

Tags:

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

like image 450
DJ-DOO Avatar asked Oct 23 '13 12:10

DJ-DOO


1 Answers

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.

like image 56
Jay Lieske Avatar answered Sep 20 '22 21:09

Jay Lieske