gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:53:50 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com