Google Maps CameraUpdateFactory not initialized

前端 未结 5 1615
忘掉有多难
忘掉有多难 2021-01-07 18:12

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

Fatal Exception java.lang.NullPointerException         
CameraU         


        
相关标签:
5条回答
  • 2021-01-07 18:15

    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.

    0 讨论(0)
  • 2021-01-07 18:18

    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.

    0 讨论(0)
  • 2021-01-07 18:20

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

    0 讨论(0)
  • 2021-01-07 18:21

    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);
                                }
                            }
                        });
                    }
                }
            }
    
        }
    
    0 讨论(0)
  • 2021-01-07 18:39

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

    0 讨论(0)
提交回复
热议问题