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

  • Home
  • SEARCH
  • 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 6225989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:56:42+00:00 2026-05-24T08:56:42+00:00

I have the following piece of code: public class DumpLocationLog extends Thread { LocationManager

  • 0

I have the following piece of code:

public class DumpLocationLog extends Thread {
    LocationManager lm;
    LocationHelper loc;
    public void onCreate() {
        loc = new LocationHelper();
        lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
    }
    public void run() {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
    }
}

I want it to be run from a remote service but in the line lm = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE); I get a NullPointerException error of course, because the context is null.

How can I get the context here? getBaseContext() or getApplicationContext() does not work.

  • 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-24T08:56:43+00:00Added an answer on May 24, 2026 at 8:56 am

    A Thread would not have any direct access to a Context by default; you would have to pass it in. A Thread also does not need an onCreate method (which I guess you are calling manually) – I would just change it to a constructor. You can just pass in a Context in the constructor of the Thread:

    public class DumpLocationLog extends Thread {
        LocationManager lm;
        LocationHelper loc;
        public DumpLocationLog(Context context) {
            loc = new LocationHelper();
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        }
        public void run() {
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, loc);
        }
    }
    

    You would instantiate it with new DumpLocationLog(this); if used from inside a Service (a Service is a subclass of Context, so this works here). You start the thread by calling the start() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code: class ICookable { public: virtual void CookMe
I have the following piece of code: class Student { public: Student(){} void display()
I have the following piece of code: class Foo { public Foo() { Bar
I have the following piece of code in my DataModel.cs class: public User ValidateUser(string
Let's say we have the following piece of code: public class Event { }
I have the following piece of code: public interface Segment<T> extends Period { ...
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =
I have a piece of code like the following: public class ActivityHelper { public
Suppose I have the following piece of code public synchronized void method() { if(something
I have the following sample piece of code, public class test implements Runnable{ public

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.