-1

In my app I am checking GPS strength. I get an java.lang.IllegalArgumentException:

listener==null.

I did not assign code listener to be null, and I write the code in the OnCreate() method. I don't know why listener turns null. Where am I going wrong?

My code:

public class Gps_strength extends Activity{
    private LocationManager mLocationManager;
    private LocationListener mGpsLocationListener;
    private GpsStatus.Listener gps_listener;

    public void onCreate(Bundle savedInstanceState) {
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGpsLocationListener);  // line no 31.
        mLocationManager.addGpsStatusListener(gps_listener);
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true) {
          ..... GPS strength checking coding..
        }
    }
}

Logcat:

    06-03 13:04:30.695: ERROR/AndroidRuntime(2595): Caused by:     java.lang.IllegalArgumentException: listener==null
    06-03 13:04:30.695: ERROR/AndroidRuntime(2595):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:627)
    06-03 13:04:30.695: ERROR/AndroidRuntime(2595):     at com.gps.Gps_strength.onCreate(Gps_strength.java:31)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

3 Answers3

0

You pass gps_listener as an argument to mLocationManager.addGpsStatusListener(), but you never assign it to a valid object, so it is set to null.

Gabriel Negut
  • 13,860
  • 4
  • 38
  • 45
  • i set as mLocationManager.addGpsStatusListener(gps_listener); in code also. i post what i have code first. please assist me. – M.A.Murali Jun 03 '11 at 09:13
  • If you don't need to be notified when the GPS status changes, just comment out the `mLocationManager.addGpsStatusListener(gps_listener);`. – Gabriel Negut Jun 03 '11 at 09:20
  • i get the same error on the same line `mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,mGpsLocationListener);` after comment out the `mLocationManager.addGpsStatusListener(gps_listener);` – M.A.Murali Jun 03 '11 at 09:30
  • It's the same issue, `mGpsLocationListener` is `null`. You should have `mGpsLocationListener = new LocationListener { /* implementation here */ }` – Gabriel Negut Jun 03 '11 at 09:45
  • yes i have coding as you told.expect declaration,every thing i wrote within oncreate method. – M.A.Murali Jun 03 '11 at 10:03
  • Edit your question and post the updated code, so we can see what's still wrong. – Gabriel Negut Jun 03 '11 at 13:47
0

You don't appear to be ever setting mGpsLocationListener to equal anything.


Alter your declations to:

private LocationListener mGpsLocationListener = new LocationListener()
{

  public void onLocationChanged(Location arg0)
  {
  }

  public void onProviderDisabled(String provider)
  {
  }

  public void onProviderEnabled(String provider)
  {
  }

  public void onStatusChanged(String provider, int status, Bundle extras)
  {
  }

};

private GpsStatus.Listener gps_listener = new GpsStatus.Listener()
{
  public void onGpsStatusChanged(int event)
  {
  }
};
Ben Williams
  • 6,027
  • 2
  • 30
  • 54
0

You have written private GpsStatus.Listener gps_listener; The above statemnets is same as private GpsStatus.Listener gps_listener = null; but in the code you have shown in nowhere the value of gps_listener is assigned. Directly you are assigning gps_listener to mLocationManager.addGpsStatusListener(gps_listener);

First assign then use.

You can find sample code here http://hejp.co.uk/android/android-gps-example/

or

How can I check the current status of the GPS receiver?

Thanks Deepak

Community
  • 1
  • 1
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243