【问题标题】:Location Listener in Android gives java.lang.IllegalArgumentException: Invalid listener : nullAndroid 中的位置侦听器给出 java.lang.IllegalArgumentException: Invalid listener : null
【发布时间】:2014-09-10 06:42:25
【问题描述】:

嘿,当我运行应用程序时,它给出了一个错误 java.lang.IllegalArgumentException: Invalid listener : null ,这表明 listener 为 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;
    }
}

【问题讨论】:

  • 您在哪里使用了 locationlistenter 功能?
  • 所以 locationlistener 为空?在传递给方法之前尝试实例化,new LocationListener();

标签: java android


【解决方案1】:

还将locationListener初始化为:

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

    }

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


    }
};

【讨论】:

    【解决方案2】:

    改变这一行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-25
      • 2011-10-17
      • 2012-08-26
      • 2017-11-05
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 2016-01-20
      相关资源
      最近更新 更多