Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - Unable to get the gps location on the emulator
    primarykey
    data
    text
    <p>i'm tring to use the gps on the android emulator, i've the following code:</p> <pre><code>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(); } } </code></pre> <p>i've added the following line at the manifest:</p> <pre><code>&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt; </code></pre> <p>and i've set the gps location with the DDMS method and either with the geo fix method, but when i run the code, i get a NullPointerExeption at the Toast line, probably cause loc is null.</p> <p>I don't understand where the error is... can you help me please?</p> <hr> <p>UPDATE!</p> <p>Thanks for your help, now i use the following code and i don't get any error, but it doesn't run the code inside onChangeLocation... it doesn't run the Toast and don't return any message in the log!</p> <pre><code> 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); } } </code></pre> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload