Google Maps - I am trying to update the current location but my App is closed as soon as I open it. Here is the code-

时间:2016-11-09 06:52:59

标签: java android maps

Every Time I open the App it says unfortunately your App has been stopped. I am not able to find the bug

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    protected LocationManager locationManager;
    TextView tv1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, (android.location.LocationListener) mLocationListener);


    }
    private final LocationListener mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            //your code here
            tv1 = (TextView) findViewById(R.id.tv1);
            tv1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
        }
    };

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);
    }
}

Here is the log Error -

java.lang.RuntimeException: Unable to start activity ComponentInfo{manishsaran.maper/manishsaran.maper.MapsActivity}: java.lang.ClassCastException: manishsaran.maper.MapsActivity$1 cannot be cast to android.location.LocationListener

1 个答案:

答案 0 :(得分:1)

问题是您的LocationListener,您应该通过

导入它
import android.location.LocationListener;

并实现其所有方法:

private final LocationListener mLocationListener = new LocationListener() {
    @Override
    public void onLocationChanged(final Location location) {
        //your code here
        tv1 = (TextView) findViewById(R.id.tv1);
        tv1.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
    }

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

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
};

并且无需转换locationListener:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0,  mLocationListener);
相关问题