java - 谷歌地图 - 我正在尝试更新当前位置,但我的应用程序一打开就关闭了。这是代码-

标签 java android maps

每次我打开该应用程序时,它都说很遗憾您的应用程序已停止。 我找不到错误

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);
    }
}

这是日志错误-

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

最佳答案

问题是您应该通过 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);

关于java - 谷歌地图 - 我正在尝试更新当前位置,但我的应用程序一打开就关闭了。这是代码-,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40501458/

相关文章:

click - Openlayers如何处理 map onclick和同时绘制问题

java - 基于缩放确定 map 上哪些点应该可见的算法

java - JSF2.0 buildView() 和 f :ajax performance

android - 无法在 Android 中添加地理围栏,因为 "network location is disabled"

Java - 多个返回语句 - 第一个不返回?

android - 为 Android Studio 设置 Google Instrumentation Test Runner

java - 使用java通过tcp发送结构化数据的最简单方法是什么?

java - 简单查询 : not implemented by SQLite JDBC driver

java - 如何判断obj可以在for循环中使用

javascript - document.ready 中 window.load 事件的 domlistener