【发布时间】:2021-02-06 01:04:55
【问题描述】:
当我在 Android 6.0 设备或 Android 10 中运行启用了应用位置功能的应用时,一切正常,但当我在 Android 7.1 设备中运行它时,它在用户授予位置权限后立即崩溃,并显示以下消息:
Process: com.hkc.incidencias, PID: 6853
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:304)
at android.location.LocationManager$ListenerTransport.-wrap0(LocationManager.java)
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:242)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
My Fragment 实现了LocationListener,它负责在请求位置更新之前检查用户权限。
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
// Start updating the user location with a minimum update time and distance set so it doesn't affect too much the battery life.
// Every time the position of the user changes, the onLocationChanged function will be called
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 20000, 10, this);
}
然后它会覆盖 onLocationChanged:
// The position of the user has changed
@Override
public void onLocationChanged(@NonNull Location location) {
// Store the user position
myPosition = new LatLng(location.getLatitude(), location.getLongitude());
...
}
【问题讨论】: