abhishek ameta
abhishek ameta

Reputation: 2416

Location listener in android gives java.lang.IllegalArgumentException: listener==null

Hey While I am running the application it gives a error java.lang.IllegalArgumentException: listener==null , that tells that listener is null.

My sample code is here:

public class HelloAndroidGpsActivity extends Activity {
private EditText editTextShowLocation;
private Button buttonGetLocation;
private LocationManager locManager;
private LocationListener locListener;
private Location mobileLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editTextShowLocation = (EditText) findViewById(R.id.editTextShowLocation);
    buttonGetLocation = (Button) findViewById(R.id.buttonGetLocation);  
    buttonGetLocation.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            buttonGetLocationClick();
        }
    });
}

/** Gets the current location and update the mobileLocation variable*/
private void getCurrentLocation() {
    locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    System.out.println("mobile location manager is ="+locManager);
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
    locListener = new LocationListener() {  
        @Override
        public void onStatusChanged(String provider, int status,
                Bundle extras) {
            System.out.println("mobile location is in listener1");
        }
        @Override
        public void onProviderEnabled(String provider) {
            System.out.println("mobile location is in listener2");
        }
        @Override
        public void onProviderDisabled(String provider) {
            System.out.println("mobile location is in listener3");
        }
        @Override
        public void onLocationChanged(Location location) {
            System.out.println("mobile location is in listener="+location);
            mobileLocation = location;
        }
    };
    System.out.println("after setting listener");
}
private void buttonGetLocationClick() {
    getCurrentLocation(); 
    System.out.println("mobile location is ="+mobileLocation);  
    if (mobileLocation != null) {
        locManager.removeUpdates(locListener); 
        String londitude = "Londitude: " + mobileLocation.getLongitude();
        String latitude = "Latitude: " + mobileLocation.getLatitude();
        String altitiude = "Altitiude: " + mobileLocation.getAltitude();
        String accuracy = "Accuracy: " + mobileLocation.getAccuracy();
        String time = "Time: " + mobileLocation.getTime();
        editTextShowLocation.setText(londitude + "\n" + latitude + "\n"
                + altitiude + "\n" + accuracy + "\n" + time);
    } else {
        editTextShowLocation.setText("Sorry, location is not determined");
    }
}
}

Output in textbox is "Sorry, location is not determined"" If any one can tell me what is the problem then please help me. Thank you

Upvotes: 1

Views: 2255

Answers (1)

Midhu
Midhu

Reputation: 1697

Initialize the listener before you use it.

locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

At this point of time, locListener is null, and you are initializing it after this line of code. This may be the reason.

So rearrange the lines of your code like this;

   locListener = new LocationListener() {...};
   locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);

Hope this may help to solve your issue...

Upvotes: 3

Related Questions