android - Google Maps CameraUpdateFactory 未初始化

标签 android google-maps

我通过 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 获得了一个非空的 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 获得异常。添加调用后,CameraUpdateFactory 始终成功。

关于android - Google Maps CameraUpdateFactory 未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19541915/

相关文章:

android - 设置 'ANDROID_HOME' 以在 Android 设备上运行 Ionic 应用程序

Java Android : Subclass, 从主 Activity 类调用方法导致 NullPointerException

android - 在我的应用程序中集成 FB SDK 时,我得到 java.lang.classnotfoundexception

ios - IOS 中 Google map 上的路线

JavaScript 可以在 Firefox 中运行,但不能在 IE 或 Chrome 中运行

JavaScript - 来自国家/地区名称的 Google API lat/lng

iphone - iPad 应用程序中的自定义 map

Android:实现只加载新页面的ViewPager

java - ARCore ArSceneView 捕获位图

android - MKCoordinateRegionMakeWithDistance 在 Android 中等效