无法解析符号“ locationListener”-Android

时间:2019-10-08 14:52:33

标签: android location locationlistener

我在实施LocationListener时遇到麻烦。

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    TextView latitudeTV;
    TextView longitudeTV;
    TextView altitudeTV;
    TextView locationNameTV;
    double latitude;
    double longitude;
    double altitude;
    String locationName;

    LocationManager locationManager;
    Location location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitudeTV = (TextView) findViewById(R.id.latitude);
        longitudeTV = (TextView) findViewById(R.id.longitude);
        altitudeTV = (TextView) findViewById(R.id.altitude);
        locationNameTV = (TextView) findViewById(R.id.locationName);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        onLocationChanged(location);

        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                longitude = location.getLongitude();
                latitude = location.getLatitude();
                altitude = location.getAltitude();
                locationName = "Placeholder";

                longitudeTV.setText("Longitude: \n" + longitude + "°");
                latitudeTV.setText("Latitude: \n" + latitude + "°");
                altitudeTV.setText("Altitude: \n" + altitude + "m");
                locationNameTV.setText("Location: \n" + locationName);
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };
    }
}

我不确定这是语法问题还是范围问题,但我不断收到“无法解析符号”错误。就像我还没有实现LocationListener方法一样。我尝试将implements LocationListener添加到MainActivity,这表明我没有实现LocationListener方法,这显示了一个错误。我不确定我的错误在哪里。

编辑:

我已根据您的回答更改了代码。当我尝试实现//locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this);时,尝试启动该应用程序时,我得到以下信息:LocationTestApp keeps stopping。我已经注释掉了这一行,该应用程序又可以正常工作了。

public class MainActivity extends AppCompatActivity implements LocationListener {

    TextView latitudeTV;
    TextView longitudeTV;
    TextView altitudeTV;
    double latitude;
    double longitude;
    double altitude;

    LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitudeTV = (TextView) findViewById(R.id.latitude);
        longitudeTV = (TextView) findViewById(R.id.longitude);
        altitudeTV = (TextView) findViewById(R.id.altitude);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }

        //locationManager.requestLocationUpdates(Context.LOCATION_SERVICE, 5000, 0, this);

        Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

        onLocationChanged(location);

    }

    @Override
    public void onLocationChanged(Location location) {
        longitude = location.getLongitude();
        latitude = location.getLatitude();
        altitude = location.getAltitude();

        longitudeTV.setText("Longitude: \n" + longitude + "°");
        latitudeTV.setText("Latitude: \n" + latitude + "°");
        altitudeTV.setText("Altitude: \n" + altitude + "m");
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

1 个答案:

答案 0 :(得分:1)

LocationListener类(在这种情况下为Activity)应定义如下:

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

public class MainActivity extends AppCompatActivity implements LocationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ...
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, this);  // Note: You pass this, so the overridden methods
    }

    @Override
    public void onLocationChanged(Location location) {
        // Do your stuff here
    }


    @Override
    public void onProviderDisabled(String provider) {}

    @Override
    public void onProviderEnabled(String provider) {}

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

这是jle评论中的解决方案。

但是,如果要使用在onCreate中声明的locationListener,则应在使用它之前声明它,方法是:

...onCreate(...) {
    ...
    LocationListener locationListener = new LocationListener() {...} // Define this...

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);   // ...and then call this

    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    onLocationChanged(location); 
}