Google Maps CameraUpdateFactory未初始化

DJ-*_*DOO 22 android google-maps

我通过crashlytics从用户体验中得到一些报告给我一个错误

Fatal Exception java.lang.NullPointerException         
CameraUpdateFactory is not initialized
Run Code Online (Sandbox Code Playgroud)

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

我曾经读过,如果地图尚未初始化,我认为我已经覆盖过这种情况

if(googleMap!=null){
                googleMap.animateCamera(CameraUpdateFactory.newLatLng(selectedLatLng));
            }  
Run Code Online (Sandbox Code Playgroud)

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

   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.");
    }
Run Code Online (Sandbox Code Playgroud)

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

如果有人对为什么会发生这种情况有任何想法我会很感激您的意见

Jay*_*ske 23

我得到了同样的错误,即使我从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);
}
Run Code Online (Sandbox Code Playgroud)

在我将调用添加到MapsInitializer之前,我会从CameraUpdateFactory中获得异常.添加调用后,CameraUpdateFactory始终成功.


Xin*_*ang 11

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


joh*_*p3r 0

也许你应该做一个Class.forName()?为了CameraUpdateFactory?在try - catch