Android中的位置监听器给出了java.lang.IllegalArgumentException:无效的监听器:null

时间:2014-09-10 06:42:25

标签: java android

嘿当我运行应用程序时,它会给出错误java.lang.IllegalArgumentException:无效的侦听器:null,它告诉侦听器为null。我是初学者所以请任何人帮助解决这个问题。我在这行中遇到错误:locationManager.requestLocationUpdates(provider,2000,0,locationListener); //我的示例代码在这里:

    public class MainActivity extends MapActivity  {
    GoogleMap map;
    MapController mapController;
    LocationListener locationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MapView myMapView = (MapView)findViewById(R.id.mapview);
        mapController = myMapView.getController();
        myMapView.setSatellite(true);
        myMapView.setStreetView(true);
        myMapView.displayZoomControls(false);
        mapController.setZoom(16);

        LocationManager locationManager;
        String context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager)getSystemService(context);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
         Location location = locationManager.getLastKnownLocation(provider);

         updateWithNewLocation(location);
       locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);

    }
    private void updateWithNewLocation(Location location) {

        // TODO Auto-generated method stub
        String latLongString;
        TextView myLocationText;
        myLocationText = (TextView)findViewById(R.id.maptextview);

        String addressString = "No Address Found";

        if(location != null)
        {
            Double geoLat = location.getLatitude()*1E6;
            Double geoLng = location.getLongitude()*1E6;

            GeoPoint point = new GeoPoint(geoLat.intValue(),geoLng.intValue());


            mapController.animateTo(point);
            Double lat = location.getLatitude();
            Double lng = location.getLongitude();

            latLongString ="Lat : "+lat+ "\n Long : "+lng;

            Double latitude = location.getLatitude();
            Double longitude = location.getLongitude();

            Geocoder gc = new Geocoder(this, Locale.getDefault());

            try
            {
                List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
                StringBuilder sb = new StringBuilder();
                if(addresses.size() > 0)
                {
                    Address address = addresses.get(0);
                    for(int i=0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName()).append("\n");

                }
                addressString = sb.toString();
            }
            catch(IOException e){ }
        }
        else {
            latLongString = "No Location Found";
        }
        myLocationText.setText("your current position : "+latLongString+"\n"+addressString);        
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}

2 个答案:

答案 0 :(得分:1)

同时将locationListener初始化为:

locationListener = new LocationListener() {
    // @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO locationListenerGPS onStatusChanged

    }

    // @Override
    public void onLocationChanged(Location location) {


    }
};

答案 1 :(得分:1)

更改此行:

locationManager.requestLocationUpdates(provider, 2000, 0, locationListener);

进入这个:

locationManager.requestLocationUpdates(provider, 2000, 0, new LocationListener(){
    // @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO locationListenerGPS onStatusChanged

    }

    // @Override
    public void onLocationChanged(Location location) {


    }
});