Google Map return 空对象引用,当 map 在 Android 中的 recyclerview 上实现时
Google Map return null object reference, when map implementing on recylerview in Android
我正在 recylerview 适配器上实现 google 映射,但它 return 空对象引用。
尝试在空对象引用上调用虚方法'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)'
public class TripAdapterViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
TripAdapterBinding tripAdapterBinding;
MapView mapView;
GoogleMap gMap;
TripAdapterViewHolder(TripAdapterBinding tripAdapterBinding) {
super(tripAdapterBinding.itemTrip);
this.tripAdapterBinding = tripAdapterBinding;
setMap(tripAdapterBinding.map);
}
public void setMap(MapView map) {
mapView = map;
if (mapView != null)
{
mapView.onCreate(null);
mapView.onResume();
mapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.gMap = googleMap;
}
我打电话给我的持有人在地图上显示数据
@Override
public void onBindViewHolder(@NonNull TripAdapter.TripAdapterViewHolder holder, int position) {
try {
GoogleMap thisMap = holder.gMap;
LatLng startPoint = new LatLng(37.7750, 122.4183);
thisMap.addMarker(new MarkerOptions()
.position(startPoint)
.title("")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
thisMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startPoint, 12));
}catch (Exception e){
Log.e("MapException "," "+e.getMessage());
}
holder.bindTrip(tripLists.get(position));
}
您应该使用显示路径的静态地图。
Static map image generate
在这里您将获得可用于在回收站视图适配器中显示的地图图像。
我正在 recylerview 适配器上实现 google 映射,但它 return 空对象引用。
尝试在空对象引用上调用虚方法'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)'
public class TripAdapterViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
TripAdapterBinding tripAdapterBinding;
MapView mapView;
GoogleMap gMap;
TripAdapterViewHolder(TripAdapterBinding tripAdapterBinding) {
super(tripAdapterBinding.itemTrip);
this.tripAdapterBinding = tripAdapterBinding;
setMap(tripAdapterBinding.map);
}
public void setMap(MapView map) {
mapView = map;
if (mapView != null)
{
mapView.onCreate(null);
mapView.onResume();
mapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
this.gMap = googleMap;
}
我打电话给我的持有人在地图上显示数据
@Override
public void onBindViewHolder(@NonNull TripAdapter.TripAdapterViewHolder holder, int position) {
try {
GoogleMap thisMap = holder.gMap;
LatLng startPoint = new LatLng(37.7750, 122.4183);
thisMap.addMarker(new MarkerOptions()
.position(startPoint)
.title("")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
thisMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startPoint, 12));
}catch (Exception e){
Log.e("MapException "," "+e.getMessage());
}
holder.bindTrip(tripLists.get(position));
}
您应该使用显示路径的静态地图。
Static map image generate 在这里您将获得可用于在回收站视图适配器中显示的地图图像。