• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Eclipse doesn't like this code. Is Eclipse right?

glenlee

Lurker
Eclipse and I aren't getting along. I have the following code (no where near completed), all of which is copied/pasted in from tutorials. Eclipse doesn't like it:

Code:
package us.glenedwards.postaltrack;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class MyLocation extends Service {

  LocationListener locationListener = new LocationListener();
  LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

  
  @Override
  public IBinder onBind(Intent intent) {
	  // TODO Auto-generated method stub
	  return null;
  }
  public void onCreate() {}

  void myLocation() {
    }
	
    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {    
	  myLocation();
	  return START_STICKY;
    }

    public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		
	}
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

}

In the line:
Code:
LocationListener locationListener = new LocationListener();
Eclipse is generating a "Cannot instantiate the type LocationListener" error.

And the line:
Code:
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
Eclipse is generating a "Syntax error on tokens, Constructor header name expected instead"

When I make the requested changes, new errors pop up.

If Eclipse is right, please point me to a Location Listener tutorial with a fully operating Class that I can copy/paste into Eclipse as is so I can see from A - Z how it's done.

If Eclipse is wrong, please tell me how to fix it.
 
Eclipse is right. You cant call any methods right in the class. You can only call methods from within another method.

You should change this:
Code:
LocationListener locationListener = new LocationListener();
To this:
Code:
LocationListener locationListener = null;
And instead instantiate the locationlistener either in onCreate or onStartCommand, and you should move these:
Code:
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
inside onCreate or onStartCommand but after the instantiatation of the locationlistener.
 
Also, im not positive on this, and I am on mobile, so I dont want to look it up ATM, but I believe that LocationListener is an interface, not a class. If this is the case, then you cannot instantiate it directly. You either need to instantiate it while overriding the methods via an inner class, or subclass it and instantiate the subclass.
 
Back
Top Bottom