无法解析符号“ locationListener”-Android

问题描述 投票:0回答:1

我在实现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, locationListener);时,该应用在启动时开始关闭/崩溃。我已注释掉导致应用程序从正常运行变为崩溃的两行内容。

public class MainActivity extends AppCompatActivity implements LocationListener {

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

    LocationManager locationManager;
    //LocationListener locationListener;

    @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, locationListener);

        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) {

    }
}
android location locationlistener
1个回答
0
投票

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); 
}
© www.soinside.com 2019 - 2024. All rights reserved.