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 6060753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:51:39+00:00 2026-05-23T08:51:39+00:00

I have a class which extends android.app.Application , which I use to persist global

  • 0

I have a class which extends android.app.Application, which I use to persist global state around my application.

I want to start off a service when my application starts, so inside the constructor of this GlobalState class I try to create an intent and start a service, but I can’t create an intent because I can’t get hold of Context

public GlobalState() {
    Log.d(this.getClass().getSimpleName(), "Initialise DatabaseManager");
    dbManager = new DatabaseManager(this);

    Log.d(this.getClass().getSimpleName(), "Requesting start up of ContactsUpdater Service");
    Intent i = new Intent(this, ContactsUpdater.class);
    startService(i);
}

I’ve tried using getApplicationContext(), but this throws a null pointer exception.

java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
at android.content.ComponentName.(ComponentName.java:75)
at android.content.Intent.(Intent.java:2551)
at com.jameselsey.apps.cercademi.domain.GlobalState.(GlobalState.java:48)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1479)
at android.app.Instrumentation.newApplication(Instrumentation.java:957)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:518)

I’m confused, I can create the DatabaseManager fine using this..

Any ideas?

  • 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-23T08:51:39+00:00Added an answer on May 23, 2026 at 8:51 am

    For Activity, Service, ContentProvider and Application, you should not do anything in the constructor. The first place you should do work, when you know the object is initialized and ready for use, is onCreate().

    Further, please think again about “I want to start off a service when my application starts.” What you code is doing here is trying to start a service when your process happens to start. I really don’t think you want that. You want this service to start because you happened to receive a broadcast in the background?

    If you just want to do some first time init, my recommendation is to not use Application at all. Have a singleton that can be retrieved when it is needed. Then your init happens at the point it is actually needed. There is no need for this to be associated with a service; you can just make a thread. The only reason to use a Service is to tell the system “my app is busy doing background work that the user cares about, please try not to kill me.”

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

Sidebar

Related Questions

I have two Java classes: B, which extends another class A, as follows :
I have a common class say for eg Class A which extends AsyncTask and
I'm writing an Android app. I have a main method, which creates and runs
i have a simple activity program in android. basically the class just extends Activity.
I have an Android app which by preference uses external storage if available to
I am developing an android application in which i have various views.When i apply
I have a class which I'm serialising to send over a unix socket and
I have a class which implements UserControl. In .NET 2005, a Dispose method is
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();

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.