首页 > 解决方案 > 在 Android 中的 recylerview 上实现地图时,Google Map 返回空对象引用

问题描述

我正在 recylerview 适配器上实现谷歌地图,但它返回空对象引用。

尝试在一个空对象引用

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));
    }

标签: androidgoogle-mapsmvvmandroid-databinding

解决方案


您应该通过显示路径来使用静态地图。

静态地图图像生成 在这里您将获得地图图像,您可以使用它在回收站视图适配器中显示。


推荐阅读