【问题标题】:On Android 7.1 (API 25) LocationManager crashes with Fatal Exception in android.location.LocationListener.onStatusChanged在 Android 7.1 (API 25) 上,LocationManager 在 android.location.LocationListener.onStatusChanged 中崩溃并出现致命异常
【发布时间】: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());
   
    ...
}

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    事实证明,在 build.gradle (:app) 中,如果我同时拥有 compileSdkVersiontargetSdkVersion 且值为 30,则代码编译得很好。但是如果我将这些值更改为29,代码将无法编译,说我需要实现LocationListener的以下方法

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

    通过此更改,即使在返回到 API 30 之后,它也可以在所有测试设备中使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 2015-07-02
      • 1970-01-01
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多