I\'m getting some reports back from user experience through crashlytics giving me an error
Fatal Exception java.lang.NullPointerException
CameraU
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.
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);
}
}
});
}
}
}
}
some time it may be caused by old version of Google Play service please Update Google play service it may helps you