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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:22:39+00:00 2026-06-14T23:22:39+00:00

This is a known annoying 2-years-old Android bug. My question is does anyone know

  • 0

This is a known annoying 2-years-old Android bug.

My question is does anyone know any workaround for this problem besides modifying Android source code and compiling it again?

Here’s my code for the sake of completion:

My Service subclass method that raises the NPE:

/** Shows notification of started service */
private void doStartForeground() {

    // Prepare notification
    final NotificationHelper nh = doNotification("Service started");

    // Start foreground
    startForeground(nh.getNotificationId(), nh.getNotification());
}

This is called from onCreate() method override.

And the JUnit test method:

public void test1() throws InterruptedException {
    assertTrue(context != null);
    final Intent service = new Intent();
    service.setComponent(new ComponentName(CoreService.PACKAGE_NAME,
        CoreService.SERVICE_FULL_NAME));
    IBinder binder = bindService(service);
    assertTrue(binder != null);
}

The stack trace:

java.lang.NullPointerException
at android.app.Service.startForeground(Service.java:631)
at com.blablabla.android.core.CoreService.doStartForeground(CoreService.java:82)
at com.blablabla.android.core.CoreService.onCreate(CoreService.java:149)
at android.test.ServiceTestCase.bindService(ServiceTestCase.java:234)
at com.blablabla.android.core.test.MainTest.test1(MainTest.java:37)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)

The Service starts correctly and works fine when not run through Android’s JUnit.

  • 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-14T23:22:41+00:00Added an answer on June 14, 2026 at 11:22 pm

    I finally ended up adding a new static field in my Service:

    /** JUNIT - this is for testing purposes only */
    public static boolean isJUnit = false;
    

    And check it in the onCreate() method:

    // Start the service as foreground (Android should not kill this
    // service)
    if (!isJUnit) {
        doStartForeground();
    }
    

    Then I set this flag from JUnit’s setUp():

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        // More setup
        MyServiceClass.isJUnit = true;
    }
    

    Not most elegant solution but gets me out of the blockade.

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

Sidebar

Related Questions

I know this seams a trivial question, but how can I disable the annoying
I think most of us know about this annoying bug in IE7 where the
This is annoying - I know how I'd do it in web... I have
Is this a known issue? I had trouble finding any search results. When iterating
Sorry if this is a known bug but I've been trying to figure this
Update : a co-worker told me that this is a known bug in the
Premise This problem has a known solution (shown below actually), I'm just wondering if
I know this question has been asked a million times in various ways by
I know this isn't strictly a programming question but y'all must have experienced this.
UPDATE 2 This is a known bug/feature with the way Ruby 1.9.2 loads files.

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.