无法转换为android中的android.location.LocationListener

问题描述

我正在尝试在我的应用程序中访问GPS。 我已授予许可,但仍然出现错误。

如果我要导入而不是 import com.google.android.gms.location.LocationListener import android.location.LocationListener;

mLocService.requestLocationUpdates(mLocProvider.getName(),getGpsUpdatePeriod(prefs),getGpsDistanceUpdatePeriod(prefs),(android.location.LocationListener)此);

代码正在编译,但是没有进入if()条件,并且“ gps权限---> 2”没有登录。

import com.google.android.gms.location.LocationListener;


public class MainActivity extends RoboActivity implements ObdProgressListener,LocationListener,GpsStatus.Listener {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gpsStart();
}

 private synchronized void gpsStart() {
    if (!mGpsIsStarted && mLocProvider != null && mLocService != null && mLocService.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d(TAG,"gps permission--->1");
        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions,and then overriding
            //   public void onRequestPermissionsResult(int requestCode,String[] permissions,//                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
           Log.d(TAG,"gps permission--->2");
            mLocService.requestLocationUpdates(mLocProvider.getName(),getGpsUpdatePeriod(prefs),getGpsDistanceUpdatePeriod(prefs),(com.google.android.gms.location.LocationListener) this);
            mGpsIsStarted = true;
            return;
        }
 } else {
        Log.d(TAG,"gps permission--->3");
        gpsStatusTextView.setText(getString(R.string.status_gps_no_support));
    }
}



 

在Android中将第4个参数投射到“ android.location.LocationListener”

如果我像下面这样,

mLocService.requestLocationUpdates(mLocProvider.getName(),getGpsUpdatePeriod(prefs),getGpsDistanceUpdatePeriod(prefs),(android.location.LocationListener)此);

获取错误**无法转换为android.location.LocationListener **

解决方法

似乎requestLocationUpdates()期望将android.location.LocationListener作为第4个参数,但是您正在传递给它一个com.google.android.gms.location.LocationListener,尽管它具有相同的名称(软件包除外),但完全不同课。

所以基本上,这就像在需要String时传递Integer一样。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...