gpt4 book ai didi

java - GPS 应用程序可以在模拟器上运行,但不能在我的手机上运行

转载 作者:行者123 更新时间:2023-12-02 08:40:50 25 4
gpt4 key购买 nike

我的基本位置应用程序无法在运行 API 29 的 Galaxy A20 上运行,但在运行相同 API 的所有模拟器上运行良好。我不确定问题是什么,因为我没有使用 LOGCAT 的经验,但错误状态如下:

尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”。

我的项目中的错误出现在第 117 行(最后)。但我在这里强调了它

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

LocationManager locationManager;
LocationListener locationListener;


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == 1){


if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

if (ContextCompat.checkSelfPermission(this , Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)

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

}


}

}

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


/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

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

locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {


LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("Marker in user house"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));

}

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

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}
};

if (ContextCompat.checkSelfPermission(this , Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED){

ActivityCompat.requestPermissions(this , new String[]{Manifest.permission.ACCESS_FINE_LOCATION} , 1);

}
else{

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

Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

LatLng userLocation = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()); // ◅ THE ERROR OCCOURS HERE********
mMap.clear();
mMap.addMarker(new MarkerOptions().position(userLocation).title("Marker in user house"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLocation));


}



}
}

最佳答案

也许您的手机尚未成功修复 GPS。使用前检查 lastKnowLocation 是否为 null,并在手机可以“看到”天空的外部测试应用程序。

关于java - GPS 应用程序可以在模拟器上运行,但不能在我的手机上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61398959/

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