gpt4 book ai didi

android - java.lang.NullPointerException : CameraUpdateFactory is not initialized, 在 "onResume"上调用方法

转载 作者:搜寻专家 更新时间:2023-11-01 08:47:35 35 4
gpt4 key购买 nike

我将从 Detail Activity 获取数据,因此我在“onResume”上更新 map ,它在 animateCamera 方法上崩溃。我尝试了 SO 中所有可用的解决方案。请在下面找到代码:

异常:java.lang.NullPointerException:CameraUpdateFactory 未初始化

private void zoomToShowAllMarkers(final GoogleMap map) {

final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (MapLocations marker : arrMarkers) {
final LatLng pos = new LatLng(marker.getLatitude(),marker.getLongitude());
builder.include(pos);
map.addMarker(new MarkerOptions().position(pos));

map.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 17.0f));

}
}


public void onResume() {
super.onResume();
if(mapView!=null && mapView.isShown())
mapView.onResume();
zoomToShowAllMarkers(map);
}

最佳答案

来自 http://developer.android.com/reference/com/google/android/gms/maps/CameraUpdateFactory.html ,

Prior to using any methods from this class, you must do one of the following to ensure that this class is initialized:

  • Wait for a GoogleMap to become available from a MapFragment or MapView that you have added to your application. You can verify that the GoogleMap is available by calling the getMap() method and checking that the returned object is not null.
  • Call initialize(Context). As long as a GooglePlayServicesNotAvailableException isn't thrown, this class will be correctly initialized.

您必须确保它已初始化,然后才能调用它的任何方法。

关于android - java.lang.NullPointerException : CameraUpdateFactory is not initialized, 在 "onResume"上调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26730013/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com