我正在尝试使用GoogleApiClient查找设备的最后已知位置,因此我使用了精细级别访问,但因为位置来自null而获得以下异常。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kuldeep.location2, PID: 1421
java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.example.kuldeep.location2.MainActivity.onConnected(MainActivity.java:51)
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle) {
try {
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
Toast.makeText(this, mLastLocation.getLatitude() + ", " + mLastLocation.getLongitude(), Toast.LENGTH_SHORT);
} catch (SecurityException e){
e.printStackTrace();
}
}
我的清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kuldeep.location2">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>
答案 0 :(得分:0)
首先,getLastLocation()可以为null,因此您可以小心地直接访问mLastLocation而不进行“null”检查。第二,我建议你添加你的清单:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
然后调用LocationRequest。
if(locRequest== null)
{
locRequest= new LocationRequest();
locRequest.setInterval( interval );
locRequest.setFastestInterval( fastestInterval );
locRequest.setPriority( priority );
LocationServices.FusedLocationApi.requestLocationUpdates(yourGoogleApiClient, locRequest, this);
}
答案 1 :(得分:0)
使用 GoogleApiClient 找到类似以下的CurrentLocation:
private GoogleApiClient googleApiClient;
private Location mLastLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isCheckLocationServiceisOn(context);
if(googleApiClient == null) {
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
protected void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
protected void onResume() {
super.onResume();
isCheckLocationServiceisOn(context);
}
@Override
public void onConnected(Bundle arg0) {
if(googleApiClient.isConnected()) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
if (mLastLocation != null) {
System.out.println("latiTude....."+ String.valueOf(mLastLocation.getLatitude()));
System.out.println("longiTude....."+ String.valueOf(mLastLocation.getLongitude()));
}
}
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
System.out.println("onConnectionFailed...."+ arg0.toString());
}
@Override
public void onConnectionSuspended(int arg0) {
System.out.println("onConnectionSuspended...."+ arg0);
}
在清单中添加权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在这里,
为支持的覆盖方法实现 ConnectionCallbacks , OnConnectionFailedListener 接口,它们必须 com.google.android.gms.common.api.GoogleApiClient < / em> 包。
必须记住:
如果没有,则必须在设备中启用Wi-Fi和GSM和GPS,然后在 onConnected()
中生成这类错误我给你代码检查&amp;开放式服务也是:
public static void isCheckLocationServiceisOn (final Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
boolean network_enabled = false;
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
catch(Exception ex) {}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
catch(Exception ex) {}
if(!gps_enabled && !network_enabled) {
// notify user
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setMessage("Location Services Disabled. \n Please enable location services.");
dialog.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
Intent myIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(myIntent);
//get gps
}
});
dialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
}
});
dialog.show();
}
}