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

If I try to use it, I get a NullPointerException . If it is deduced from the constructor, it works fine. So, I'm curious what is going on.

Here is the 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 the output of logcat when I start the 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โˆ—11)
 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โˆ—51)
 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

There are also a bunch of related questions here on SO

+4
source share
2 answers

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

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

+14
source

Try using the handler from onCreate () or the service designer and do your work inside the handler.

-one
source

Source: https://habr.com/ru/post/1496990/


All Articles