Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use Context.getPackageManager inside Service's constructor

Tags:

android

If I try to use it, I get NullPointerException. If moved out of constructor, it works fine. So, I'm curious what's going on.

Here is code:

package com.example.nullptrservice;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;

public class MyService extends Service {
    public MyService() {
        super();
        PackageManager pm = this.getPackageManager();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
        Log.i("MyService", "---------- started --------");
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) { return null; }
}

And this is logcat output, when I run service manually:

D/AndroidRuntime( 4174): 
D/AndroidRuntime( 4174):  AndroidRuntime START com.android.internal.os.RuntimeInit 
D/AndroidRuntime( 4174): CheckJNI is OFF
D/AndroidRuntime( 4174): Calling main entry com.android.commands.am.Am
D/dalvikvm( 4184): Late-enabling CheckJNI
D/AndroidRuntime( 4174): Shutting down VM
I/ActivityManager(  148): Start proc com.example.nullptrservice for service com.example.nullptrservice/.MyService: pid=4184 uid=10066 gids={}
I/AndroidRuntime( 4174): NOTE: attach of thread 'Binder Thread #3' failed
D/dalvikvm( 4174): GC_CONCURRENT freed 103K, 81% free 489K/2560K, paused 1ms+1ms
D/jdwp    ( 4174): Got wake-up signal, bailing out of select
D/dalvikvm( 4174): Debugger has detached; object registry had 1 entries
D/AndroidRuntime( 4184): Shutting down VM
W/dalvikvm( 4184): threadid=1: thread exiting with uncaught exception (group=0x409ee1f8)
E/AndroidRuntime( 4184): FATAL EXCEPTION: main
E/AndroidRuntime( 4184): java.lang.RuntimeException: Unable to instantiate service com.example.nullptrservice.MyService: java.lang.NullPointerException
E/AndroidRuntime( 4184):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2237)
E/AndroidRuntime( 4184):    at android.app.ActivityThread.access$1600(ActivityThread.java:123)
E/AndroidRuntime( 4184):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
E/AndroidRuntime( 4184):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4184):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 4184):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 4184):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4184):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4184):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 4184):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 4184):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4184): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 4184):    at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)
E/AndroidRuntime( 4184):    at com.example.nullptrservice.MyService.(MyService.java:12)
E/AndroidRuntime( 4184):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime( 4184):    at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime( 4184):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2234)
E/AndroidRuntime( 4184):    ... 10 more

Android 4.0.3

Also there is a bunch of related questions here on SO

like image 388
Alexander Malakhov Avatar asked Aug 14 '13 09:08

Alexander Malakhov


1 Answers

You cannot use a Service (or an Activity for that matter) as a Context when the object is being created/initialized.

Instead, override onCreate() and do your initialization there and get rid of the explicit constructor.

like image 173
laalto Avatar answered Oct 04 '22 22:10

laalto