“类型中的方法不适用于参数”错误

时间:2013-11-26 13:43:02

标签: android google-maps

下面的代码应该跟踪用户的当前位置,但是“updatePlaces(this);”给出了错误“MainActivity.CustomMapFragment类型中的方法updatePlaces(Context)不适用于参数(MainActivity.CustomMapFragment)”。我对这一切还很陌生,我无法理解这个错误。我怎么能解决它?

/**
 * A fragment that displays the map.
 */
public static class CustomMapFragment extends Fragment implements
        LocationListener {

    private MapView mapView;
    private GoogleMap map;
    private int userIcon, AedValidatedIcon, AedNotValidatedIcon;
    private LocationManager locMan;
    private Marker userMarker;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Icons
        userIcon = R.drawable.blue_point;
        AedValidatedIcon = R.drawable.yellow_point;
        AedNotValidatedIcon = R.drawable.red_point;

        // Inflate and return the layout
        View v = inflater.inflate(R.layout.map_fragment, container, false);
        mapView = (MapView) v.findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.onResume();// needed to get the map to display immediately

        map = mapView.getMap();
        map.getUiSettings().setAllGesturesEnabled(false);
        map.getUiSettings().setZoomControlsEnabled(false);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        updatePlaces(this);

        return v;
    }

    private void updatePlaces(Context c)
    {
        locMan = (LocationManager)c.getSystemService(LOCATION_SERVICE);
        Location lastLoc = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        double lat = lastLoc.getLatitude();
        double lng = lastLoc.getLongitude();

        LatLng lastLatLng = new LatLng(lat, lng);

        if (userMarker!=null) userMarker.remove();

        userMarker = map.addMarker(new MarkerOptions()
            .position(lastLatLng)
            .title("You are here")
            .icon(BitmapDescriptorFactory.fromResource(userIcon))
            .snippet("Your last recorder location"));

        map.animateCamera(CameraUpdateFactory.newLatLng(lastLatLng), 3000, null);
        locMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30000, 50, this);
    }

2 个答案:

答案 0 :(得分:3)

替换此

updatePlaces(this);

通过

updatePlaces(getActivity());

public final Activity getActivity ()

返回当前与Activity相关联的fragment

答案 1 :(得分:1)

由于Fragment不是Context,您需要调用ActivityFragment与此相关联:

updatePlaces(this.getActivity());