在我的主要活动中,我有一个按钮,按下按钮就会启动一个新的MapActivity。 目标是让MapActivity加载并在屏幕上显示一个地图,该地图在地图上的用户当前位置有一个指针。
当按下按钮时,应用程序崩溃,我已经尝试了很多次来解决这个问题但是4小时后我无处可去!
public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private LocationManager locationManager;
private String provider;
private Location location;
private GoogleMap mMap;
private int MY_PERMISSIONS_REQUEST_FINE_LOCATION= 1;
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
mMap = googleMap;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
location = getMyLocation();
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(),location.getLongitude())).title("My location"));
}
public Location getMyLocation() {
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_FINE_LOCATION);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
if (location != null)
{
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 13));
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets the center of the map to location user
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(40) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
return location;
}}
按下按钮时发生的崩溃日志:
05-21 01:07:24.379 30002-30002/com1032.cw2.jm01061.jm01061_assignmentv2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com1032.cw2.jm01061.jm01061_assignmentv2, PID: 30002
java.lang.RuntimeException: Unable to start activity ComponentInfo{com1032.cw2.jm01061.jm01061_assignmentv2/com1032.cw2.jm01061.jm01061_assignmentv2.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5822)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com1032.cw2.jm01061.jm01061_assignmentv2.MapActivity.onCreate(MapActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6308)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2543)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2678)