-2
私はGoogleマップ上で私の現在の場所を見つけることができるはずの小さなモバイルアプリケーションを作成しています。 私はそれが以前に働いていたので、ボタンの1つをクリックすることができ、現在の位置にズームしました。 は現在、いくつかの理由イムのために以下に掲載され、それは私のコードの行53の方に指して、次のエラーLocation.getLongitude()nullオブジェクト参照
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference
取得:double myLongitude = myLocation.getLongitude();
ないアイデア理由:
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と、を指し、これが起こっている 助けていただければ幸いです!