Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:17:24+00:00 2026-05-26T04:17:24+00:00

I have a problem here and I cant get it to work… I want

  • 0

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.

I read that this is maybe because I have to pass the Context to the 2nd class but I dont know how… please help!

Here comes the code:

GPSActivity.class:

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();
    }
  }
}

(I stripped the code a bit)

Now, I can call showCurrentLocation and it showes me the long and lat values.

How can I do this here:

SmsReceive.class (when a sms is received, send the gps coordinates)

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();
    }
  }
}

thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:17:25+00:00Added an answer on May 26, 2026 at 4:17 am

    declare a new locationManager in the smsReceiver class and use the following lines

    mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
     Location loc = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a bigSmall problem here. I cant open any type of project in visual
I have a problem here. My Zend_Forms do not render in view script. Via
Using Rational ClearCase v. 7.0.1.1 with UCM, I have a problem here when using
I have an interesting problem here I've been trying to solve for the last
I have a bit of an architecture problem here. Say I have two tables,
I have run into a bit of a problem here: I had a problem-specific
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
I'm having a bit of a problem here. We have 2 urls let me
I've got an interesting box-model problem here. I have a header full of links,
I'm battling a really weird problem here. I have a Windows 2008 R2 server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.