java - Android 应用程序 - 定位服务的@Override 方法出错

标签 java android android-gps

我正在尝试编写一个用于交付的 Android 应用程序。很大一部分是找到移动设备的当前位置。

我遵循的所有教程都给出了我无法弄清楚或理解的相同错误,我是 Android 开发的新手。

我收到的错误是“Error:(76, 5) error: method does not override or implement a method from a supertype”。

我的代码:

public class LoginActivity extends AppCompatActivity {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
String tempLocation;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;

public static String pName;
public static String pID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);


    //Get Input
    etUsername = (EditText) findViewById(R.id.txtLoginUserName);
    etPassword = (EditText) findViewById(R.id.txtLoginPassword);




    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

}
@Override
public void onLocationChanged(Location location) {
    tempLocation = "Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude();
    TextView lg = (TextView) findViewById(R.id.txtLoginUserName);
    lg.setText(tempLocation);
}

@Override
public void onProviderDisabled(String provider) {
    Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
    Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Log.d("Latitude","status");
}

还有我的结果日志:

Error:(76, 5) error: method does not override or implement a method from a supertype
Error:(83, 5) error: method does not override or implement a method from a supertype
Error:(88, 5) error: method does not override or implement a method from a supertype
Error:(93, 5) error: method does not override or implement a method from a supertype
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.661 secs
Information:5 errors
Information:0 warnings
Information:See complete output in console

我正在使用这个 tutorial .

任何帮助或建议将不胜感激,因为我尝试使用许多不同的答案但到目前为止没有真正帮助。

最佳答案

您的类中缺少以下 implements LocationListener...

使用这样的东西

class LocationActivity extends AppCompatActivity implements LocationListener{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onLocationChanged(Location location) {

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


}

然后你将能够实现你想要的......希望它对你有帮助

关于java - Android 应用程序 - 定位服务的@Override 方法出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44815566/

相关文章:

android - 开始 Activity 在堆栈上添加另一个 Activity

android - PWA 已经支持 GPS 了吗?

使用 Google API 的 Android 融合位置

java - 使用 ANT 并遇到问题

java - 使用 java Date 和 SimpleDateFormat 验证范围

java - 即使在 Java 中出现编译错误,我也能够创建一个类对象

java - Spock 模拟验证返回 0 次调用

android - 使用 navigateUpFromSameTask() 从 Activity 返回

java - Android OnDraw 和 onTouchEvent 不能一起工作

c# - xamarin 中构建的 Android 后台位置正在永远占用