[英]SmartLocationLibrary location is always null
我正在嘗試學習Android的SmartLocationLibrary來獲取位置,並且從中獲取的位置始終為null,因此在嘗試對其進行操作時會出現null對象引用錯誤。
我為SmartLocatonLibrary創建了一個Utility類
public class LocationUtil implements OnLocationUpdatedListener, OnActivityUpdatedListener {
private final String TAG = "LocationUtil: ";
private Location location;
private DetectedActivity activity;
private LocationGooglePlayServicesProvider provider;
private Context context;
public LocationUtil(Context context) {
this.context = context;
}
/**
* If lastLocation is not null then the location is set to last location recorded by phone
*/
public void getLastLocation(){
Location lastLocation = SmartLocation.with(context).location().getLastLocation();
if(lastLocation != null){
location = lastLocation;
}
DetectedActivity detectedActivity = SmartLocation.with(context).activity().getLastActivity();
if(detectedActivity != null) activity = detectedActivity;
}
/**
* Basically this starts the location tracking
* If you want one tick of location use .oneFix() on the smartLocation builder
* If you want continous tracking just don't add the .oneFix()
*/
public void startLocation(){
provider = new LocationGooglePlayServicesProvider();
provider.setCheckLocationSettings(true);
SmartLocation smartLocation = new SmartLocation.Builder(context).logging(true).build();
smartLocation.location(provider)
.continuous()
.start(this);
smartLocation.activity()
.start(this);
}
/**
* This stops the tracking
*/
public void stopLocation(){
SmartLocation.with(context).location().stop();
Log.d(TAG, "stopLocation: Location Stopped");
SmartLocation.with(context).activity().stop();
Log.d(TAG, "stopLocation: Activity Recognition Stopped");
}
@Override
public void onActivityUpdated(DetectedActivity detectedActivity) {
this.activity = detectedActivity;
}
@Override
public void onLocationUpdated(Location location) {
this.location = location;
}
//--------SETTERS----------
public void setProvider(LocationGooglePlayServicesProvider provider) {
this.provider = provider;
}
//--------GETTERS----------
public Location getLocation() {
return location;
}
public LocationGooglePlayServicesProvider getProvider() {
return provider;
}
}
這是使用它的Maps Activity
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private final String TAG = "MAPY";
private int LOCATION_PERMISSION_ID = 1001;
private GoogleMap mMap;
Location location;
LocationUtil locationUtil = new LocationUtil(MapsActivity.this);
Button centerButton;
@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);
centerButton = findViewById(R.id.centerCameraBtn);
centerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(location != null)
mMap.moveCamera((CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude()))));
}
});
}
@Override
protected void onStop() {
super.onStop();
locationUtil.stopLocation();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//Check for permissions if they are not granted request them
if (ContextCompat.checkSelfPermission(MapsActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MapsActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_ID);
return;
} else{
locationUtil.startLocation();
}
locationUtil.getLastLocation();
Log.d(TAG, "onMapsReady.lat: " + location.getLatitude() + " onMapsReady.lng: " + location.getLongitude());
location = locationUtil.getLocation();
LatLng currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(currentLocation).title("My current location").snippet("This is a sinppet"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(currentLocation));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(locationUtil.getProvider() != null){
locationUtil.getProvider().onActivityResult(requestCode, resultCode, data);
}
}
}
我做到了,就像這里顯示的庫的作者一樣,但是它不起作用。 有趣的是,它昨天有效。 我正在我的個人電話上對其進行測試。 位置,移動數據和wifi均已打開且工作正常。 我獲得了權限,我將權限添加到了清單(FINE和COARSED),並且在我的依賴項中
實現'com.android.support:appcompat-v7:28.0.0'實現'com.android.support:support-v4:28.0.0'實現'com.android.support:support-media-compat:28.0.0'實施'io.nlopez.smartlocation:library:3.3.3'實施'com.google.android.gms:play-services-maps:16.0.0'實施'com.google.android.gms:play-services-location: 16.0.0'testImplementation'junit:junit:4.12'androidTestImplementation'com.android.support.test:runner:1.0.2'androidTestImplementation'com.android.support.test.espresso:espresso-core:3.0.2'實現' com.android.support.constraint:約束的布局:1.1.3'
我真的不知道發生了什么事,請幫幫我。
確定,所以這不起作用,因為我想記錄尚未初始化的位置變量的值。 使用調試器對其進行調試表明我正在獲取位置。 但是更有趣的是,因為這是一個僅在主應用程序中測試庫的項目,即使我所做的一切都與此處相同,相同的Utility類也不會獲取我的位置...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.