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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:45:29+00:00 2026-06-11T01:45:29+00:00

On an application I’m working on the onCreate starts a service with startService() and

  • 0

On an application I’m working on the onCreate starts a service with startService() and subsequently after, that same activity calls bindService(to that same service) in its onStart. The problem is the Sevice isn’t initialized until well after the onStart has completed which causes a fatal error from a nullpointer exception caused by a null service. I don’t know how to get around this without using a half-assed solution and I have no idea how the onStart is finishing before the onCreate.

Suggestions? Any help is much appreciated.

public class PNetworkService extends Service {
private final IBinder binder = new NtwkSvcBinder(this);


@Override
public void onCreate() {
    // Creates a HandlerThread to process the network events.
    HandlerThread handlerThread = new HandlerThread("SystemStartService",
            Process.THREAD_PRIORITY_BACKGROUND);
    // Starts the network thread.
    handlerThread.start();
    // Obtains looper so that that the NetworkServiceHandler is bound to that looper.
    serviceLooper = handlerThread.getLooper();
    // Create the Network Service Handler.
    nsh = new NetworkServiceHandler(serviceLooper, this);
    messenger = new Messenger(nsh);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand");
    return START_STICKY;
}

@Override
public IBinder onBind(Intent i) {
    Log.i(TAG, "onBind called");
    return binder;
}

public void setExtHandler(Handler extHandler) {
    Log.i(TAG, "setting exthandler");
        nsh.setExtHandler(extHandler);
}
}
  • 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-06-11T01:45:31+00:00Added an answer on June 11, 2026 at 1:45 am

    I think that the main problem is that you assume that the service is already running when Activity’s onStart is executing and expect bindService to bind immediately. However, usually it doesn’t work.

    The reason of such behavior, I think, is that Android instantiates Components one by one, think you cannot synchronously create service inside onCreate of Activity. For sure it will be created, but after the system queue will go to this new service creation.

    So will it work if you’ll just provide a correct handling of Connection for binding the service? For instance move following code

    mConnection.getService().setExtHandler(mapActivityHandler);
    

    to inside of onServiceConnected() ?

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

Sidebar

Related Questions

My application has one activity which starts two services but does not bind them.
Application db connection in web service. i haveto connect the db using that web
Application : HTA (therefore IE) This is an application that uses SendKeys to populate
Application : I am working on one mid-large size application which will be used
my application use 10 threads that to read a lot of html file.similar the
My application that is not on Play Store verify on the web If there
Application is a C# .Net 3.5 WCF Service. I'd like during the build process
APPLICATION DESCRIPTION : I am a new iPhone developer. I am working on an
Application.Run(new Main()); This line gives TypeInitializationException was unhandled after I switched from 3.5 to
Application use NHibernate. I Have object A that contains set of objects B. I

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.