Unable to override methods of LocationListener class

百般思念 提交于 2019-12-07 08:39:13

问题


I want to get current location using GPS.

Here is my scenario. My class definition is:

public class UseGps extends Activity implements LocationListener

If Í try to override the LocationListener methods like this

1)

@Override 
public void onLocationChanged(Location loc)

2)

@Override
public void onProviderDisabled(String provider)

3)

@Override
public void onProviderEnabled(String provider)

it is giving me compilation error saying

The method onStatusChanged(String, int, Bundle) of type UseGps must override a superclass method remove override.

Same for 2) and 3)

What is wrong here?


回答1:


Assuming that you are using Eclipse do the following to ensure that your compliance level is 1.6.

Go to Project -> Properties -> Java Compiler. There make sure that Compiler compliance level is set to 1.6.

You should set that level as a default value by navigating to Window -> Preferences -> Java -> Compiler. Select 1.6 for Compiler compliance level.




回答2:


Someone should probably explain that:

  1. @Override wasn't supported for interfaces until Java 1.6.

  2. In Java 1.5, you cannot use @Override on a method to indicate it is intended to implement an interface method - the compiler will flag it as an override error. Under Java 1.5, you can use @Override only to indicate that the method is overriding a superclass method.

  3. Starting in Java 1.6, you can use @Override to indicate the method is intended to implement an interface method (too bad it wasn't a separate annotation like @Implements).

  4. Eclipse has a feature that can ensure your code compiles under a particular version of Java, regardless of what actual version of Java you have installed - this is the Compiler Compliance Level. If it is set to 1.5, even if your JDK is 1.6, the compiler will flag any code that would not compile under Java 1.5.



来源:https://stackoverflow.com/questions/4883496/unable-to-override-methods-of-locationlistener-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!