我正在使用位置代码将设备的纬度和经度在以下设备上正常工作: HTC One M8 Moto X 2ndG NexusP One Plus 2
但它在三星Galaxy S6中崩溃了
这是我正在使用的位置代码。
public static ParseGeoPoint geoPointFromLocation(Location loc) {
return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude());
}
myLoc = (MainActivity.currentLocation == null) ? MainActivity.lastLocation : MainActivity.currentLocation;
around.setLocation(MainActivity.geoPointFromLocation(myLoc));
Logcat已添加:
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at io.wyntr.peepster.Activities.MainActivity.geoPointFromLocation(MainActivity.java:399)
at io.wyntr.peepster.Activities.RecipientsActivity$2.onClick(RecipientsActivity.java:124)
at android.view.View.performClick(View.java:5697)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
MainActivity中使用的位置代码:
boolean hasSetUpInitialLocation;
public static Location lastLocation;
public static Location currentLocation;
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
private static GoogleApiClient GoogleApiClient;
// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 1000;
// The update interval
private static final int UPDATE_INTERVAL_IN_SECONDS = 5;
// A fast interval ceiling
private static final int FAST_CEILING_IN_SECONDS = 1;
// Update interval in milliseconds
private static final long UPDATE_INTERVAL_IN_MILLISECONDS = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
// A fast ceiling of update intervals, used when the app is visible
private static final long FAST_INTERVAL_CEILING_IN_MILLISECONDS = MILLISECONDS_PER_SECOND
* FAST_CEILING_IN_SECONDS;
ParseUser user = ParseUser.getCurrentUser();
// A request to connect to Location Services
private LocationRequest locationRequest;
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationRequest = LocationRequest.create();
coordinatorLayout = (CoordinatorLayout) findViewById(R.id
.coordinatorLayout);
if (AppStatus.getInstance(this).isOnline()) {
} else {
TSnackbar snackbar = TSnackbar.make(coordinatorLayout,"No Internet Connection",TSnackbar.LENGTH_LONG);
snackbar.show();
}
// Set the update interval
locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
// Use high accuracy
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
locationRequest.setFastestInterval(FAST_INTERVAL_CEILING_IN_MILLISECONDS);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
requestLocationPermissions();
}
if (checkPlayServices()) {
buildGoogleApiClient();
}
@Override
public void onConnected(Bundle bundle) {
currentLocation = getLocation();
try{startPeriodicUpdates();} catch (Exception e){
requestLocationPermissions();
}
}
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
return true;
// Google Play services was not available for some reason
} else {
// Display an error dialog
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0);
if (dialog != null) {
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
errorFragment.setDialog(dialog);
errorFragment.show(getSupportFragmentManager(), "Wyntr-Beta");
}
return false;
}
}
@Override
public void onConnectionSuspended(int i) {
snackbar = TSnackbar
.make(coordinatorLayout,"Connection Suspended",TSnackbar.LENGTH_SHORT);
snackbar.show();
}
private void startPeriodicUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
requestLocationPermissions();
}
try{LocationServices.FusedLocationApi.requestLocationUpdates(
GoogleApiClient, locationRequest, this);} catch(Exception e){
requestLocationPermissions();
}
}
private Location getLocation() {
// If Google Play Services is available
if (servicesConnected()) {
// Get the current location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
requestLocationPermissions();
}
return LocationServices.FusedLocationApi.getLastLocation(GoogleApiClient);
} else {
return null;
}
}
@Override
public void onStart() {
super.onStart();
// Connect to the location services client
GoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
GoogleApiClient.disconnect();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
snackbar = TSnackbar
.make(coordinatorLayout, "Your Android Device is not Supported", TSnackbar.LENGTH_SHORT);
snackbar.show();
finish();
}
return false;
}
return true;
}
protected synchronized void buildGoogleApiClient() {
GoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
public static ParseGeoPoint geoPointFromLocation(Location loc) {
return new ParseGeoPoint(loc.getLatitude(), loc.getLongitude());
}
我有位置代码。我正在使用的PlayServices已被取消,但它在其他设备上运行。它不仅仅适用于三星Galaxy设备。
答案 0 :(得分:0)
如果您的 Samsung S6 已升级为 Android Marshmallow 6 ,那么我想您的问题是权限问题,请考虑在Android 6的运行时请求权限启用位置。
请检查https://developer.android.com/training/permissions/requesting.html 这是关于如何在运行时请求权限
此链接https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous有助于了解新的权限类型。