首页 > 解决方案 > 移动到单独的班级时,此处不允许注释

问题描述

我尝试将我的位置查找代码与主要活动分开,因此我将整个相关代码移至不同的类:(*我今天开始使用 Android,Java 对我来说也相当新,如果这是一个愚蠢的问题,请见谅)

public class LocationCode {

    private LocationCallback mLocationCallback;
    private TextView tv;
    private Button button;
    private FusedLocationProviderClient mFusedLocationClient;
    private final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5;
    public LocationCode (Context context, TextView textView, Button btn) {

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(10000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        this.tv = textView;
        this.button = btn;
        mLocationCallback = new LocationCallback() {
            @Override
            public void onLocationResult(LocationResult locationResult) {
                if (locationResult == null) {
                    return;
                }
                for (Location location : locationResult.getLocations()) {
                    // Update UI with location data
                    Double latDouble = location.getLatitude();
                    String latString = latDouble.toString();
                    Double longtDouble = location.getLongitude();
                    String longtString = longtDouble.toString();
                    tv.setText(latString + " " + longtString);

                }
            }
        };
        @Override
        public void onRequestPermissionsResult ( int requestCode,
        String permissions[], int[] grantResults){
            TextView tv1 = this.tv;
            Button button = this.button;
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                                != PackageManager.PERMISSION_GRANTED) {
                            //
                        } else {
                            mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
                                    null /* Looper */);
                        }

                    } else {
                        // permission denied, boo! Disable the
                        // functionality that depends on this permission.
                        tv1.setText("Not Granted!");
                        button.setVisibility(View.VISIBLE);

                    }
                    return;
                }

                // other 'case' lines to check for other
                // permissions this app might request.
            }
        }

    }   


}

从 MainActivity 我可能应该这样称呼这段代码: LocationCode locationCode = new LocationCode(this, textView, Button);

@Override但是我在第二个(上面的那个)上收到以下错误public void onRequestPermissionsResult。为什么会这样,我该如何解决?或者有更好的方法来分离这段代码,让它在 MainActivity 中看起来更整洁?

标签: android

解决方案


推荐阅读