我通过crashlytics从用户体验中获取了一些报告,给我一个错误

Fatal Exception java.lang.NullPointerException
CameraUpdateFactory is not initialized

这不是正常的崩溃,因为并不是每个用户都发生此崩溃,但是它变得太正常了,我需要解决它。

我读过,如果没有初始化 map ,可能会发生这种情况,我认为我已经讲过
if(googleMap!=null){
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(selectedLatLng));
            }

还有一个可能的原因可能是Google Play服务不在设备上或设备已过期,我也为此添加了一些验证。
   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.");
    }

之后,我不确定下一步该怎么做,因为每个用户都不会发生这种情况。

如果有人对为什么会发生这种情况有任何想法,我们将不胜感激

最佳答案

即使我从MapView获得了非null的GoogleMap对象,也遇到了同样的错误。我通过对MapsInitializer的显式调用来解决了该问题,即使文档中说它不是必需的。

我的应用程序的Fragment通过以下方式设置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中获得异常。添加 call 后,CameraUpdateFactory总是成功。

07-24 09:21