在requestLocationUpdates上的Android java.lang.AbstractMethodError [英] Android java.lang.AbstractMethodError on requestLocationUpdates
问题描述
我收到此错误 java.lang.AbstractMethodError:抽象方法"void android.location.LocationListener.onProviderDisabled(java.lang.String)
当我调用 locationManager.requestLocationUpdates()
时.我看到了另一个帖子,答案是要覆盖某些方法,即
when i call locationManager.requestLocationUpdates()
. I saw another post where the answer was to override some methods i.e.
@Override
public void onProviderDisabled(@NonNull String provider) {}
但是问题是我无法在项目的任何地方覆盖它们.
But the thing is i cannot override them anywhere in my project.
我已经简化了代码,以帮助读者,而不是我没有两个班,而是一个班
I have simplified the code in order to help the readers and instead of two classes i have one
MainActivity:
public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener = location -> Toast.makeText(MainActivity.this,location.getLatitude()+" "+location.getLongitude(),Toast.LENGTH_LONG).show();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
findViewById(R.id.button).setOnClickListener(v -> showLoc());
findViewById(R.id.button2).setOnClickListener(v -> locationManager.removeUpdates(locationListener));
}
private void showLoc(){
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 13);
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 13) {
for (int i = 0; i < permissions.length; i++) {
if (permissions[i].equals(Manifest.permission.ACCESS_FINE_LOCATION) && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
System.out.println("DEBUG Request Permissions ");
showLoc();
}
}
}
}
}
该异常抛出在 locationManager.requestLocationUpdates
上,并且与库版本有关.
The exception is thrown at locationManager.requestLocationUpdates
and has something to do with library version.
最后这是我的 build.gradle(app)
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 25
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
推荐答案
找到了解决方案.在 android 10(Q) 之后,您将不再需要 LocationListener
的许多未实现的方法,例如 onProviderDisabled
.
Found the solution. After android 10(Q) you won't need lot's of the the unimplemented methods of LocationListener
like onProviderDisabled
.
但是我们的大多数应用具有很高的利润率",具有向后兼容性的功能,支持许多较旧的Android版本,因此您需要覆盖这些版本.
But most of our apps have high "margin" of backwards compatibility supporting much older Android versions so you will need to override these ones.
因此,为了实现这些功能,我们可以通过两种方法来实现:
So in order to implement these we can do this by two approaches:
第一个解决方案是创建一个新的LocationListener作为变量,并立即实现这些方法
First solution is creating a new LocationListener as a variable and implement these methods straight away
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
System.out.println("DEBUG 1");
Toast.makeText(MainActivity.this,location.getLatitude()+" "+location.getLongitude(),Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(@NonNull String provider) {
System.out.println("DEBUG 2");
Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(@NonNull String provider) {
System.out.println("DEBUG 3");
Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
System.out.println("DEBUG 4");
Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_LONG).show();
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0.5f, locationListener);
第二种方法是在您的类中实现 LocationListener
并实现这些方法
A Second approach is to implement LocationListener
in your class and implement these methods
public class MainActivity extends AppCompatActivity implements LocationListener
现在您必须实现方法
@Override
public void onLocationChanged(@NonNull Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
您的 LocationListener
是您自己的类
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0.5f, this);
最后要记住的是,即使 public void onStatusChanged(字符串提供程序,int状态,捆绑包额外功能){}
也已被弃用,因为表示使用 Android 9或更低版本的Android手机在通过 locationManager.requestLocationUpdates
Last thing to take in mind is that even tho public void onStatusChanged(String provider, int status, Bundle extras) {}
is deprecated you SHOULD implement because as i said Android phones that use Android 9 or lower are going to trigger that method when taking location updates with locationManager.requestLocationUpdates
这篇关于在requestLocationUpdates上的Android java.lang.AbstractMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!