我在recylerview适配器上实现google map,但是它返回空对象引用。
尝试在以下位置调用虚拟方法“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;
}
我调用我的持有人在 map 上显示数据
@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));
}
最佳答案
您应该通过显示路径来使用静态 map 。
Static map image generate
在这里,您将获得 map 图像,可用于显示在回收站 View 适配器中。
关于android - 在Android中的recylerview上实现 map 时,Google Map返回空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52379780/