我使用的位置代碼來獲得其對以下設備工作正常設備的緯度和經度: 的HTC One M8 極限摩托2ndG NexusP 壹加2getLatitude和getLongitude對三星Galaxy S6崩潰
但它在三星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的補充:在MainActivity使用
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)
地點代碼:
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已被棄用,但它在其他設備上工作。它不適用於Samsung Galaxy設備。
你如何獲得位置? 您能否提供更多代碼...您的位置loc對象可能爲空。 – Seishin
Samsung Galaxy S6的Android版本是什麼? –
發佈您的logcat! – Rami