Android中的LocationListener错误无法获取用户位置

时间:2018-10-27 18:03:32

标签: android

我正在尝试在我正在构建的android应用程序中添加位置跟踪器,但出现问题,位置监听器似乎是错误的根源,logcat说无法转换为android.location.LocationListener。那我想用什么来铸造呢?还有其他选择。这是代码

public class Location extends Activity {
    Context context;
    protected LocationManager locationManager;
    protected LocationListener locationListener;

    public Location(Context context) {
        this.context = context;
    }

    @SuppressLint("MissingPermission")
    public void locate() {
        locationManager = (LocationManager) context.getSystemService( Context.LOCATION_SERVICE );
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(android.location.Location location) {
                Log.v( "location" , location.getLatitude()+""+location.getLongitude() );
            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if(Build.VERSION.SDK_INT<23){
            locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );
        }else{
            if (ActivityCompat.checkSelfPermission( context, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission( context, 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.
                ActivityCompat.requestPermissions( (Activity) context,new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1 );
                //return;
            }else{
                locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );
            }
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult( requestCode, permissions, grantResults );
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission( context,Manifest.permission.ACCESS_FINE_LOCATION )== PackageManager.PERMISSION_GRANTED){
                locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );
            }
        }
    }

}

我从哪里获得此代码?从本教程 https://www.youtube.com/watch?v=kz4wigGXilI

日志说

Caused by: java.lang.ClassCastException: com.onedevz.noct.MainActivity cannot be cast to android.location.LocationListener

2 个答案:

答案 0 :(得分:1)

实际上这很简单,您尝试将上下文传递为LocationListener。而是将locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );更改为locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, locationListener);

答案 1 :(得分:0)

您的错误在这里:

locationManager.requestLocationUpdates( 
   LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) context );
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^

context持有类型com.onedevz.noct.MainActivity的实例,该实例未实现LocationListener。将(LocationListener) context替换为locationListener,这是您的位置更改处理程序实现。