Android Google Maps CameraUpdateFactory未初始化
通过crashlytics给我一个错误,我从用户体验中得到了一些报告Android Google Maps CameraUpdateFactory未初始化,android,google-maps,Android,Google Maps,通过crashlytics给我一个错误,我从用户体验中得到了一些报告 Fatal Exception java.lang.NullPointerException CameraUpdateFactory is not initialized 这不是一个常规的崩溃,因为它不会对每个用户都发生,但它变得太常规了,我需要解决它 我曾读到,如果地图没有初始化,这可能会发生,我想我已经讨论过了 if(googleMap!=null){ googleMap
Fatal Exception java.lang.NullPointerException
CameraUpdateFactory is not initialized
这不是一个常规的崩溃,因为它不会对每个用户都发生,但它变得太常规了,我需要解决它
我曾读到,如果地图没有初始化,这可能会发生,我想我已经讨论过了
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.");
}
之后,我不确定下一步该怎么做,因为这不会发生在每个用户身上
如果有人对发生这种情况的原因有任何想法,我将非常感谢您的输入您是否应该执行
类.forName()
?对于CameraUpdateFactory
?在try-catch
中,我得到了相同的错误,尽管我从MapView中获得了一个非空的GoogleMap对象。我通过显式调用MapsInitializer解决了这个问题,尽管文档中说不需要它
我的应用程序片段通过以下方式设置地图视图:
@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始终会成功。有时可能是由于旧版本的Google Play服务造成的,请更新Google Play服务。它可能会帮助您只需使用MapsInitializer.initialize而不使用try catch,因为它不会在最新版本的Google Play services中抛出GooglePlayServicesNotAvailableException,该异常已通过软件包版本5084032进行验证。我的
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);
}
}
});
}
}
}
}
由于缺少GlobalyLayoutListenerUtil和getBetterZoom,无法完全使用上述代码-请发布缺少的代码。谢谢