estoy Tring para utilizar el GPS en el emulador de Android, he el siguiente código:Android - No se puede obtener la ubicación GPS en el emulador
public class NL extends Activity {
private LocationManager locmgr = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nl);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
crit.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locmgr.getBestProvider(crit, true);
Location loc = locmgr.getLastKnownLocation(provider);
Toast msg = Toast.makeText(this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
msg.show();
}
}
He añadido la siguiente línea en el manifiesta:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
y me he fijado la ubicación GPS con el método DDMS y, o bien con el método de solución geo, pero cuando ejecuto el código, me sale un NullPointerExeption en la línea de pan tostado, probablemente causaría loc es nulo .
No entiendo dónde está el error ... ¿me pueden ayudar, por favor?
¡ACTUALIZACIÓN!
Gracias por su ayuda, ahora uso el siguiente código y no obtengo ningún error, pero no ejecuta el código dentro deChangeLocation ... no ejecuta el Toast y no devuelve ningún mensaje en el registro!
public class NL extends Activity {
private LocationManager locmgr = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nl);
locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location loc) {
// Called when a new location is found by the network location provider.
Log.i("NOTIFICATION","onLocationChenged Triggered");
Toast msg = Toast.makeText(NetworkLocator.this, "Lon: " + Double.toString(loc.getLongitude()) + " Lat: " + Double.toString(loc.getLatitude()), Toast.LENGTH_SHORT);
msg.show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locmgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
}
¡Gracias!
tenía que comprobar si en realidad * * locmgr.getBestProvider devuelve "GPS"? de todos modos, definitivamente debe implementar un * LocationListener * y pasarlo a LocationManager con * requestLocationUpdates() * –
getBestProvider devolver "gps". ¿Tengo que agregar un locador de ubicación incluso si trato de obtener la ubicación con lastKnownLocation? gracias por su ayuda –
@Marco Faion Por favor, mantenga sus respuestas actualizadas ... ¡Ya le respondí una vez y no dio ningún comentario sobre la respuesta! > 'http: // stackoverflow.com/questions/4591765/android-sqlite-database-method-undefined-fot-type/4591843 # 4591843' – Beasly