好的,所以我在GPS.class使用这个GPS位置,我一直得到Null Object参考,不知道为什么。这是我的代码:
boolean permissionGranted = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
if(permissionGranted)
{
if (GPS.sharedInstance(AppDashboard.this).canGetLocation() && GPS.sharedInstance(this).isPermissionEnabled()) {
double lat = GPS.sharedInstance(AppDashboard.this).getLatitude();
double lon = GPS.sharedInstance(AppDashboard.this).getLongitude();
Log.e(TAG, "Location: " + lat + "/"+ lon);
}
else
{
Log.e(TAG, "Can't get GPS locations");
}
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 200);
}
我的宣言:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
我的Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
at com.gpsApp.utility.GPS.getLatitude(GPS.java:113)
GPS Class 113
public double getLatitude() {
if (_locationManager != null) {
_latitude = _location.getLatitude();
}
return _latitude;
}
答案 0 :(得分:0)
使用此行
{{ item.form_date|timesince:item.to_date }}
而不是
boolean permissionGranted = ActivityCompat.checkSelfPermission(getApplicationContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
答案 1 :(得分:0)
您正在从public double getLatitude()
类调用GPS
,但_location
变量未初始化,因此您获得NullPointerException。
在调用getLatitude()
之前,请尝试从您的public Location getLastKnownLocation()
课程中调用GPS
。此调用将初始化_location
变量。