也許有人可以告訴我爲什麼下面的代碼工作在Api上面25,但在Api 19哪一個現在我測試我有null之後,我嘗試獲取經度和緯度? 我讀過官方docs但但我用(目標阿比25)每一種方法是跟蹤API 19LoactionManager Api19 NullPointerException
Location loc = gpStracker.getlocation();
latitude = loc.getLatitude();
longitude = loc.getLongitude();
和gpsTracker類兼容:
public class GPStracker implements LocationListener {
Context context;
public GPStracker(Context c) {
context = c;
}
public Location getlocation() {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(context, "Uprawnienia nie przyznane", Toast.LENGTH_SHORT).show();
}
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean isGPSenabled;
isGPSenabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (isGPSenabled) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return location;
} else {
Toast.makeText(context, "Proszę włączyć GPS", Toast.LENGTH_LONG).show();
}
return null;
}
@Override
public void onLocationChanged(Location location) {
//Toast.makeText(context, String.valueOf(dis), Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
應該由谷歌 –