-2
即時創建一個小的移動應用程序,應該讓我能夠在谷歌地圖上找到我目前的位置。 我讓它工作得更早,我可以點擊其中一個按鈕,它放大到我當前的位置。 現在,由於某種原因,即時得到以下錯誤Location.getLongitude()null object reference
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
它朝我的代碼53行指出這是下面貼:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@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);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
double myLongitude = myLocation.getLongitude();
double myLatitude = myLocation.getLatitude();
// Create a LatLng object for the current location
LatLng latLng = new LatLng(myLongitude, myLatitude);
Marker me = mMap.addMarker(new MarkerOptions()
.position(new LatLng(myLatitude, myLongitude))
.title("Im here!")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.people))
);
// Zoom into my current location
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(myLatitude, myLongitude), 15.0f));
}
}
53號線是指:double myLongitude = myLocation.getLongitude();
不知道爲什麼這發生了 任何幫助將不勝感激!