从onBindViewHolder android访问谷歌地图

时间:2017-01-28 22:00:34

标签: android google-maps android-recyclerview recycler-adapter

我有recycle view,每个人都有一个map_view对象,我想分别访问每一行来设置市场,添加行等等#<

已查看herehere

但是当我尝试使用GoogleMap这样的对象时

holder.mMap.addMarker(new MarkerOptions()
     .position(my_data.get(position).getLatLngEvent())
     .title("test")
);

我得到了这个

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ofir.bopofinal, PID: 707
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
    at com.example.ofir.bopofinal.myRides.Passengers.passengersAdapter.onBindViewHolder(passengersAdapter.java:85)
    at com.example.ofir.bopofinal.myRides.Passengers.passengersAdapter.onBindViewHolder(passengersAdapter.java:35)
    at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
    at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
    at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
    at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
    at android.view.View.measure(View.java:17547)
    at android.widget.RelativeLayout.measureChild(RelativeLayout.java:697)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
    at android.view.View.measure(View.java:17547)
    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
    at android.view.Choreographer.doFrame(Choreographer.java:550)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invok

这是我的适配器代码:

import android.content.Context;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.ofir.bopofinal.R;
import com.example.ofir.bopofinal.myRides.rideData;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class passengersAdapter extends RecyclerView.Adapter<passengersAdapter.MyViewHolder> {

    private static Context context;
    private static List<passengersData> my_data;

    public passengersAdapter(Context context, List<passengersData> my_data) {
        this.context = context;
        this.my_data = my_data;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.passengers_card, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

     holder.userName.setText(my_data.get(position).getPassengerName());
        holder.eventName.setText(my_data.get(position).getEventName());
        holder.initializeMapView();

        //init latlang
        holder.latLngEvent = holder.getLocationFromAddress(context,my_data.get(position).getEventLocation());
        holder.latLanPickUp = holder.getLocationFromAddress(context,my_data.get(position).getPickUpLocation());
        holder.latLangStartLocation = holder.getLocationFromAddress(context,my_data.get(position).getStartLocation());
        //set markets
        holder.mMap.addMarker(new MarkerOptions().position(my_data.get(position).getLatLngEvent()).title("test"));

        holder.mCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    @Override
    public void onViewRecycled(MyViewHolder holder) {
        if (holder.mMap != null) {
            holder.mMap.clear();
            holder.mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
        }
        super.onViewRecycled(holder);
    }

    @Override
    public int getItemCount() {
        return my_data.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {

        private TextView userName, eventName;
        protected CardView mCardView;
        GoogleMap mMap;
        MapView map_view;
        public LatLng latLngEvent, latLanPickUp, latLangStartLocation;

        public MyViewHolder(View v) {
            super(v);

            userName = (TextView) v.findViewById(R.id.tvPssengersName);
            eventName  = (TextView) v.findViewById(R.id.tvPassengersEventName);
            map_view = (MapView) v.findViewById(R.id.passengersMapView);
            mCardView = (CardView) v.findViewById(R.id.passenger_card_view);
        }

        public void initializeMapView() {
            if (map_view != null) {
                map_view.onCreate(null);
                map_view.onResume();
                map_view.getMapAsync(this);
            }
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            MapsInitializer.initialize(context.getApplicationContext());
            mMap = googleMap;
            mMap.getUiSettings().setAllGesturesEnabled(false);
            GoogleMapOptions options = new GoogleMapOptions().liteMode(true);
        }

        public void setMarkers(GoogleMap map, LatLng name, String title) {
            map.addMarker(new MarkerOptions().position(name).title(title));
        }

        public void setPolyLines(GoogleMap map, LatLng start, LatLng end, int color) {
            PolylineOptions line = new PolylineOptions().add(start,end);
            line.color(color);
            map.addPolyline(line);
        }

        public LatLng getLocationFromAddress(Context context, String strAddress) {

            Geocoder coder = new Geocoder(context);
            List<Address> address;
            LatLng p1 = null;

            try {
                address = coder.getFromLocationName(strAddress, 5);
                if (address == null) {
                    return null;
                }
                Address location = address.get(0);
                location.getLatitude();
                location.getLongitude();

                p1 = new LatLng(location.getLatitude(), location.getLongitude() );

            } catch (Exception ex) {    
                ex.printStackTrace();
            }

            return p1;
        }

        private LatLng computeCentroid(List<LatLng> points) {
            double latitude = 0;
            double longitude = 0;
            int n = points.size();

            for (LatLng point : points) {
                latitude += point.latitude;
                longitude += point.longitude;
            }

            return new LatLng(latitude/n, longitude/n);
        }

        private double calculateDistance(List<LatLng> points) {
            Location loc1 = new Location("");
            loc1.setLatitude(points.get(0).latitude);
            loc1.setLongitude(points.get(0).longitude);

            Location loc2 = new Location("");
            loc2.setLatitude(points.get(1).latitude);
            loc2.setLongitude(points.get(1).longitude);

            return distance_between(loc1.getLatitude(),loc1.getLongitude(),loc2.getLatitude(),loc2.getLongitude());
        }

        double distance_between(double lat1, double lon1, double lat2, double lon2) {
            //float results[] = new float[1];
            /* Doesn't work. returns inconsistent results
            Location.distanceBetween(
            l1.getLatitude(),
            l1.getLongitude(),
            l2.getLatitude(),
            l2.getLongitude(),
            results);
            */

            double R = 6371; // km
            double dLat = (lat2-lat1)*Math.PI/180;
            double dLon = (lon2-lon1)*Math.PI/180;
            lat1 = lat1*Math.PI/180;
            lat2 = lat2*Math.PI/180;

            double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
            double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
            double d = R * c * 1000;

            return d;
        }
    }

}

1 个答案:

答案 0 :(得分:2)

您正在初始化地图并在序列中添加标记。你必须等待地图初始化。

你的onBindViewHolder应如下所示:

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.userName.setText(my_data.get(position).getPassengerName());
    holder.eventName.setText(my_data.get(position).getEventName());
    holder.initializeMapView();

   //init latlang
    holder.latLngEvent =  holder.getLocationFromAddress(context,my_data.get(position).getEventLocation());
    holder.latLanPickUp = holder.getLocationFromAddress(context,my_data.get(position).getPickUpLocation());
    holder.latLangStartLocation = holder.getLocationFromAddress(context,my_data.get(position).getStartLocation());

    holder.mCardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

和你的onMapReady:

@Override
public void onMapReady(GoogleMap googleMap) {
    MapsInitializer.initialize(context.getApplicationContext());
    mMap = googleMap;

    mMap.getUiSettings().setAllGesturesEnabled(false);
    GoogleMapOptions options = new GoogleMapOptions().liteMode(true);

    mMap.addMarker(new MarkerOptions().position(my_data.get(getAdapterPosition()).getLatLngEvent()).title("test"));
}

为了更正,我在onMapReady调用之后将调用移到了addMarker。