【发布时间】:2018-12-31 11:27:10
【问题描述】:
我正在创建一个在地图上显示标记的应用。问题是我在运行我的应用程序时收到以下错误:
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.flag.app.app.fragment.FragmentMap$1.onMapReady(FragmentMap.java:186)
at com.google.android.gms.maps.zzac.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:380)
这是我的 FragmentMap.class
公共类 FragmentMap 扩展 Fragment {
private GoogleMap googleMap;
public MapView mMapView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_map, container, false);
mMapView = rootView.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume(); // needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(new OnMapReadyCallback() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onMapReady(GoogleMap mMap) {
googleMap = mMap;
Log.d("debug", "map ready called");
LatLng lviv = new LatLng(49.838, 24.029);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lviv, 12));
if (ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d("debug", "problem");
}
List<Marker> markerList = getActivity().getIntent().getParcelableArrayListExtra(EXTRA_MARKER_LIST);
for (int i = 0; i < markerList.size(); i++) {
Marker marker = markerList.get(i);
Location location = marker.getLocation();
clusterManager.setItems(markerList);
}
}
});
return rootView;
}
【问题讨论】:
-
我认为是您的 markerList var 当您尝试获取它的大小时它们为空,它们会抛出此异常。
List<Marker> markerList = getActivity().getIntent().getParcelableArrayListExtra(EXTRA_MARKER_LIST);检查是否返回null。 -
你为什么用
MapView而不是SupportMapFrament?
标签: android google-maps firebase-realtime-database nullpointerexception marker