【问题标题】:Android java.lang.AbstractMethodError on requestLocationUpdatesAndroid java.lang.AbstractMethodError on requestLocationUpdates
【发布时间】:2021-04-18 08:38:37
【问题描述】:

我收到此错误java.lang.AbstractMethodError: abstract method "void android.location.LocationListener.onProviderDisabled(java.lang.String)

当我打电话给locationManager.requestLocationUpdates()。我看到另一个post 的答案是覆盖一些方法,即

 @Override
public void onProviderDisabled(@NonNull String provider) {}

但问题是我不能在我的项目中的任何地方覆盖它们。

为了帮助读者,我简化了代码,而不是两个类,我有一个

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抛出异常,与库版本有关。

最后这是我的 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'
}

【问题讨论】:

  • 这可能是版本冲突,即您可能正在使用具有LocationListener 实现但没有实现该抽象方法的库,因为它是使用不同版本的Android SDK 比你在 atm 上运行你的应用程序。
  • 看看AbstractMethodError在Java中的一般含义:baeldung.com/java-abstractmethoderror
  • 我已阅读该帖子。问题是,如果您看到我的问题顶部的链接,我会提到另一篇文章,其中说有一些未实现的方法。正如错误所暗示的那样,以及来自 baeldung 的文章,这是有道理的。但是当我尝试实现这些方法时,我就是做不到!在我的两门课中都没有

标签: java android locationmanager abstractmethoderror


【解决方案1】:

覆盖代码中的方法

...

override fun onProviderDisabled(provider: String) {}
override fun onProviderEnabled(provider: String) {}
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
...

【讨论】:

    【解决方案2】:

    找到了解决办法。在 android 10(Q) 之后,您将不再需要 LocationListener 的许多未实现方法,例如 onProviderDisabled

    我们的大多数应用都具有较高的向后兼容性“余量”,支持更旧的 Android 版本,因此您需要覆盖这些版本。

    因此,为了实现这些,我们可以通过两种方法来实现:

    第一个解决方案是创建一个新的 LocationListener 作为变量并立即实现这些方法

    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并实现这些方法

    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(String provider, int status, Bundle extras) {}弃用,您也应该实施,因为正如我所说,使用 Android 9 或更低版本的 Android 手机将会使用locationManager.requestLocationUpdates进行位置更新时触发该方法

    【讨论】:

    • 如果源代码是针对 compileSdkVersion 30 编译的,为什么还需要覆盖?
    猜你喜欢
    • 1970-01-01
    • 2017-10-15
    • 2019-06-29
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 2022-10-25
    • 2013-06-14
    • 2015-09-21
    相关资源
    最近更新 更多