Google Maps CameraUpdateFactory未初始化

时间:2013-10-23 12:37:04

标签: android google-maps

我通过崩溃解决方案从用户体验中获得了一些回复报告

Fatal Exception java.lang.NullPointerException         
CameraUpdateFactory is not initialized

这不是常规崩溃,因为它不是每个用户都会发生,但它变得过于规则,我需要解决它。

我已经读过,如果地图尚未初始化,我认为我已经覆盖了

,这可能会发生
if(googleMap!=null){
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(selectedLatLng));
            }  

也可能是因为谷歌播放服务不在设备上或已过时,我也为此添加了一些验证。

   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.");
    }

之后,我不确定接下来要做什么,因为每个用户都没有这样做。

如果有人对此为何发生任何疑问,我将非常感谢您的意见

5 个答案:

答案 0 :(得分:22)

即使我从MapView获取了非null的GoogleMap对象,我也遇到了同样的错误。我通过显式调用MapsInitializer解决了这个问题,即使文档说它不需要。

我的应用片段通过以下方式设置MapView:

@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);
}

在我添加对MapsInitializer的调用之前,我会从CameraUpdateFactory中获得异常。添加调用后,CameraUpdateFactory总是成功。

答案 1 :(得分:11)

只需使用MapsInitializer.initialize而不使用try-catch,因为它不会在最新版本的Google Play服务中抛出GooglePlayServicesNotAvailableException,该服务已通过软件包版本5084032进行验证。

答案 2 :(得分:1)

有些时候可能是由旧版Google Play服务引起的,请更新Google Play服务,它可能会对您有所帮助

答案 3 :(得分:0)

你应该做Class.forName()吗? CameraUpdateFactory?在try - catch

答案 4 :(得分:0)

来自MapUtil的处理此案例的内容:

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);
                            }
                        }
                    });
                }
            }
        }

    }