我有一个小问题,我的GPS追踪器一直要求激活位置,而我激活它。我分析并重新分析了我的课程,但找不到我的错误...
Someboy关心帮忙?
GPSTracker.java
public class GPSTracker extends Service implements LocationListener {
private final Context context;
Location location;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = false;
boolean canGetLocation = false;
double latitude;
double longitude;
private static final long MinDistanceChangeUpdate = 10;
private static final long MinTimeUpdate = 1000 * 60 * 1;
protected LocationManager locationManager;
public GPSTracker(Context context){
this.context = context;
getLocation();
}
public Location getLocation(){
try{
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isNetworkEnabled && !isGPSEnabled){
}else{
this.canGetLocation = true;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MinTimeUpdate, MinDistanceChangeUpdate, this);
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled){
if(location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MinTimeUpdate, MinDistanceChangeUpdate, this);
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}
return location;
}
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
return latitude;
}
public double getLongitude (){
if(location != null){
longitude = location.getLongitude();
}
return longitude;
}
public boolean canGetLocation(){
return this.canGetLocation;
}
public void showSettingsAlert(){
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("GPS is Settings! ");
alertDialog.setMessage("GPS is not enable, Wanna enable?");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
context.startActivity(intent);
}
});
alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
MainActivity.java
public class MainActivity extends Activity {
Button btnshowLocation;
GPSTracker gps;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnshowLocation = (Button)findViewById(R.id.showLocation);
btnshowLocation.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
gps = new GPSTracker(MainActivity.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Latitude = " + latitude + " Longitude = " + longitude, Toast.LENGTH_LONG).show();
} else {
gps.showSettingsAlert();
}
}
});
}
}
Stracktrace
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
04-26 23:55:51.943 21374-21374/com.example.dell.exampleapplication W/System.err﹕ at android.content.ContextWrapper.getSystemService(ContextWrapper.java:582)
04-26 23:55:51.943 21374-21374/com.example.dell.exampleapplication W/System.err﹕ at com.example.dell.exampleapplication.GPSTracker.getLocation(GPSTracker.java:44)
04-26 23:55:51.943 21374-21374/com.example.dell.exampleapplication W/System.err﹕ at com.example.dell.exampleapplication.GPSTracker.<init>(GPSTracker.java:39)
04-26 23:55:51.943 21374-21374/com.example.dell.exampleapplication W/System.err﹕ at com.example.dell.exampleapplication.MainActivity$1.onClick(MainActivity.java:28)
编辑:添加了Stracktrace 提前谢谢,
答案 0 :(得分:0)
很有趣看到我很久以前在SO回答中发布的一些代码片段:-D Location servise GPS Force closed
自那时以来发生了很多事情,我最近的建议是使用https://github.com/mcharmas/Android-ReactiveLocation
主要是因为:
要获取最新位置,您需要编写的唯一代码是:
ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context);
locationProvider.getLastKnownLocation()
.subscribe(new Action1<Location>() {
@Override
public void call(Location location) {
doSthImportantWithObtainedLocation(location);
}
});
为了模糊地回答你的问题,我的怀疑是,你可能已经打开了GPS,但没有启用WiFi位置(只是猜测)。
如果您决定使用您发布的代码,那么我建议添加各种日志输出来调试情况。类似的东西:
public Location getLocation(){
try{
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isNetworkEnabled && !isGPSEnabled){
Log.e("GPSTracker", "!isNetworkEnabled && !isGPSEnabled");
Log.e("GPSTracker", "is network location enabled: " + isNetworkEnabled );
Log.e("GPSTracker", "is GPS location enabled: " + isGPSEnabled);
}else{
this.canGetLocation = true;
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MinTimeUpdate, MinDistanceChangeUpdate, this);
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if(isGPSEnabled){
if(location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MinTimeUpdate, MinDistanceChangeUpdate, this);
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
}catch(Exception e){
Log.e("GPSTracker", "error getting location", e);
e.printStackTrace();
}
return location;
}
否则对canGetLocation布尔值施加限制性较小的约束(因为它实际上是导致问题的变量)
更改:
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(!isNetworkEnabled && !isGPSEnabled){
}else{
this.canGetLocation = true;
...
要
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
this.canGetLocation = isNetworkEnabled || isGPSEnabled;
if(!isNetworkEnabled && !isGPSEnabled){
}else{
...