タイトルが言うように、私はAndroidの開発者サイトにあるコードを守り、現在地の緯度と経度を取得しようとしています。私はAndroid Studioとエミュレータを使用しています。 getLatitude()とgetLongitude()を使用しているときに私のコードがnullを渡す理由がわかりません - 指定されたコードに従ったと思いました。Android - LocationServicesが現在の場所を取得しようとしたときにnullを渡しています
ありがとうございます!
(クラスで必要なもの)HomePage.java:
public void onConnected(@Nullable Bundle bundle) {
if (location == null){
declareLocation();
}
double latitude = location.getLatitude();
double longitude = location.getLongitude();
SharedPreferences sharedPreferences = getSharedPreferences("example.com.musicapptest", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("latitude", Double.doubleToLongBits(latitude));
editor.putLong("longitude", Double.doubleToLongBits(longitude));
}
private Location declareLocation(){
try{
location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
catch (SecurityException e){
e.printStackTrace();
}
return location;
}
可能な複製(http://stackoverflow.com/questions/16830047/locationclient-getlastlocation-return-null ) –