Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid locationManager Null Pointer Exception? (how to pass the context?)
    primarykey
    data
    text
    <p>I have a problem here and I cant get it to work... I want to get the latest gps positions but all i get is an null pointer exception. It works with the first class GPSActivity but not with the 2nd SmsReceiver.</p> <p>I read that this is maybe because I have to pass the Context to the 2nd class but I dont know how... please help!</p> <p>Here comes the code:</p> <p><code>GPSActivity.class</code>:</p> <pre><code>import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class GPSActivity extends Activity { long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in Milliseconds LocationManager locationManager; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); locationManager.getAllProviders(); locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES, MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener()); } public void showCurrentLocation() { Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { String message = String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude()); Toast.makeText(GPSActivity.this, message, Toast.LENGTH_LONG).show(); } } } </code></pre> <p>(I stripped the code a bit)</p> <p>Now, I can call showCurrentLocation and it showes me the long and lat values.</p> <p>How can I do this here:</p> <p><code>SmsReceive.class</code> (when a sms is received, send the gps coordinates)</p> <pre><code>import ... public class SmsReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { GPSActivity myGPS; myGPS = new GPSActivity(); //---get the SMS message passed in--- Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; if (bundle != null) { //HERE I GET THE NULL POINTER EXCEPTION Location loc = myGPS.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); double varLong = loc.getLongitude(); double varLat = loc.getLatitude(); String locationData = "LONG: " + varLong+ " LAT: " + varLat; Toast.makeText(context, locationData, Toast.LENGTH_SHORT).show(); } } } </code></pre> <p>thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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