protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonGps = findViewById(R.id.buttonGps);
final Context activity = this;
buttonGps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!checkForPhonePermission()) {
return;
}
LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
Log.d(TAG, location.getLatitude() + "");
}
}, Looper.myLooper());
}
});
}
使用API级别22在Android Studio中创建了一个模拟器,在授予权限后并开启模拟器的GPS后,当单击按钮时,应用程序崩溃并显示以下错误:
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onStatusChanged(java.lang.String, int, android.os.Bundle)"
如果我在关闭GPS的情况下按下按钮,则会出现以下错误:
java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)"
如果我在我的小米Mi A1上尝试这个应用程序,只有当GPS关闭并且点击按钮时它才会崩溃,在GPS打开并按下按钮时不会崩溃。
根据文档,这些方法被标记为默认,所以我不需要实现它们。
这种行为有什么原因吗?